Codeforces 570E Pig and palindromes DP

Source: Internet
Author: User

Link

Links: Click to open the link

Test instructions

The letter matrix for the given n*m.

The path from the upper-left corner to the lower-right corner is a palindrome.

Ideas:

Obviously to the head of the DP at the same time, the path 1 is from the upper left to line J, Path 2 is from the lower right corner to the K line

Dp[i][j][k] indicates that the path length is I, path 1 from the upper left to the first row J, path 2 from the lower right to the K line, and Paths 1 and 2 are the number of matching methods.

For paths 1, 2, you want to split the parity when merging.

#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <stdio.h> #include <iostream> #include <algorithm> #include <sstream> #include <stdlib.h> #include <string.h> #include &LT;LIMITS.H&G  T #include <vector> #include <string> #include <time.h> #include <math.h> #include &LT;IOMANIP&G  T #include <queue> #include <stack> #include <set> #include <map> const int inf = 1e9;const Doub Le EPS = 1e-8;const double pi = ACOs ( -1.0); template <class t>inline BOOL Rd (T &ret) {char c; int sgn;if (c = Get char (), c = = EOF) return 0;while (c! = '-' && (c< ' 0 ' | | c> ' 9 ')) C = GetChar (); sgn = (c = = '-')? -1:1;ret = (c = = '-')? 0: (C-' 0 '); while (c = GetChar (), C >= ' 0 ' &&c <= ' 9 ') ret = ret * + (C-' 0 '); ret *= Sgn;return 1;} Template <class t>inline void pt (T x) {if (x < 0) {Putchar ('-'); x =-X;} if (x > 9) pt (X/10);p Utchar (x% 10 + ' 0 ');}using namespace Std;const int N = 520;const int mod = 1e9 + 7;typedef long long ll;typedef pair<int, int> pii;void A DD (int &x, int y) {x + = y;if (x >= mod) x-= mod;} int mul (int x, int y) {x = (ll) X*y%mod;return X;} int N, M;char s[n][n];vector<pii>g[1005];int step[4][2] = {0, 1, 1, 0, 0,-1,-1, 0}, Dis[n][n];bool inmap (int x, int y) {return 1 <= x&&x <= n && 1 <= y&&y <= m;} void BFs () {queue<int>qx, Qy;qx.push (1), Qy.push (1);d is[1][1] = 1;while (!qx.empty ()) {int UX = Qx.front (), Uy = qy. Front (); G[dis[ux][uy]].push_back ({UX, uy}); Qx.pop (); Qy.pop (); for (int i = 0; i < 2; i++) {int VX = UX + step[i][0], vy = Uy + step[i][1];if (INMAP (VX, VY) &&!dis[ Vx][vy]) {Dis[vx][vy] = Dis[ux][uy] + 1;qx.push (VX); Qy.push (VY);}}} int Dp[2][n][n];int Main () {rd (N); Rd (M); for (int i = 1; I <= N; i++) scanf ("%s", S[i] + 1), if (s[1][1]! = s[n][m]) {p UTS ("0"); return 0; }S[0][1] = ' a '; S[1][0] = ' Z ' + 1;s[n + 1][m] = ' a '; S[n][m + 1] = ' Z ' + 1;BFS (); int cur = 0, old = 1;memset (Dp[cur], 0, sizeof dp[cur]);DP [cur][1][n] = 1;int L = 1, R = n + M- 1;for (int i = 2; I <= (n + m)/2; i++, l++, r--) {swap (cur, old), memset (Dp[cur], 0, sizeof dp[cur]); for (auto U:g[l] {int x = u.first, y = u.second;for (auto v:g[r]) {int tx = V.first, Ty = v.second;if (dp[old][x][tx] = = 0) continue;for ( int a = 0; A < 2;  a++) {int UX = x + step[a][0], Uy = y + step[a][1];if (!inmap (UX, UY)) continue;for (int b = 2; b < 4; b++) {int VX = tx + Step[b][0], vy = ty + step[b][1];if (!INMAP (VX, VY)) continue;if (S[ux][uy]! = S[vx][vy]) continue;add (DP[CUR][UX][VX], dp[ OLD][X][TX]);}}}} int ans = 0;if ((n + m) & 1) {for (int i = 1; I <= n; i++) Add (ans, dp[cur][i][i]); for (int i = 1; I <= n; i++) add (ans, dp[cur][i][i + 1]);} else {for (int i = 1; I <= n; i++) Add (ans, dp[cur][i][i]);} PT (ANS); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces 570E Pig and palindromes DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.