Codeforces 570 E. Pig and Palindromes

Source: Internet
Author: User

Test instructions: Give the n*m alphabet, ask from the upper left corner to the lower right corner can form how many palindrome string, can only go down or to the right.


Procedure: Dp[r1][c1][r2][c2], from the upper left corner to (R1,C1), from the lower right corner to (R2,C2), can form how many palindrome string, because burst memory, expressed as DP[STEP][R1][R2], from the upper left corner to the R1 line, from the lower right corner to the R2 line, Separate walk step, can form how many palindrome string, because C1=STEP+2-R1,C2=N+M-STEP-R2, so is the same, so almost can pass, because both sides up to walk 250 steps, so need space is 250*500*500, of course, Since step is only related to Step-1, you can also scroll the array.

#include <map> #include <string> #include <cstring> #include <cstdio> #include <cstdlib># include<cmath> #include <queue> #include <vector> #include <iostream> #include <algorithm > #include <bitset> #include <climits> #include <list> #include <iomanip> #include <stack > #include <set>using namespace std;const int Mod=1000000007;char pic[501][501];int dp[2][501][501];int main () {int n,m;scanf ("%d%d", &n,&m), for (int i=0;i<n;i++) scanf ("%s", Pic[i]), int len= (N+M)/2-1;for (int i=0;i< =len;i++) {for (int. j=1;j<=n;j++) {int c1=i+2-j;if (c1<=m) {for (int k=n;k>=j;k--) {int c2=n+m-i-k;dp[i&1][j ][k]=0;if (c2>=c1&&c2<=m) {if (pic[j-1][c1-1]==pic[k-1][c2-1]) {int id= (i&1), jd= (id^1), if (i==0) DP [ID] [j] [K]=1;else{dp[id][j][k]=dp[jd][j][k];DP [id][j][k]= (Dp[id][j][k]+dp[jd][j-1][k])%mod;dp[id][j][k]= (Dp[id][j][k] +DP[JD][J][K+1])%mod;dp[id][j][k]= (dp[id][j][k]+dp[jd][j-1][k+1])%mod;}}}}} int id= (len&1), ans=0;for (int i=1;i<=n;i++) ans= (Ans+dp[id][i][i])%mod;if ((n+m-1)%2==0) for (int i=1;i<=n;i++) ans= (ans+dp[id][i][ I+1])%mod;printf ("%d", ans);



Time limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Peppa the Pig was walking and walked into the forest. What a strange coincidence! The forest has the shape of a rectangle, consisting ofNRows andmColumns. We enumerate the rows of the rectangle from top to bottom with numbers from1ToN, and the Columns-from left-to-right with numbers from1Tom. Let's denote the cell at the intersection of theR-th Row and theC-th column as(R,? ) C).

Initially the pig stands in cell(1,?1), and in the end she wants to IS in cell(n,? M). Since the pig is in a hurry to get home, she can go from cell(R,? ) C), only to either cell(R. +?1,? C)Or(R,? ) C. +?1). She cannot leave the forest.

The Forest, where the pig is, is very unusual. Some cells of the forest similar to all other, and Some look very different. Peppa enjoys taking pictures and at every step she takes a picture of the cell where she was now. The path through the forest is considered to Bebeautiful  if Photographs taken on her, can is viewed in both forward and reverse order, showing the same sequenc E of photos. More formally, the line formed by the cells in order of visiting should be A palindrome   (you can read a formal definition of a palindrome in the previous problem).

Count the number of beautiful paths from cell (1,?1) to cell (n,? M). Since This number can is very large, determine the remainder after dividing it by 9? +?7.

Input

The First line contains Integers  n ,? m   ( 1?≤? n ,? m ? ≤?500 )-the height and width of the field.

Each of the following n lines contains m lowercase 中文版 letters Identifying the types of cells of the Forest. Identical cells is represented by identical letters, different cells is represented by different letters.

Output

Print a single integer-the number of beautiful paths modulo 9? +?7.

Sample Test (s) input
3 4aaabbaaaabba
Output
3
Note

Picture illustrating possibilities for the sample test.



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

Codeforces 570 E. Pig and Palindromes

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.