E. Pig and Palindromes
 

Peppa the Pig was walking and walked into the forest. What a strange coincidence! The forest has the shape of a rectangle, consisting of n rows and m columns. We enumerate the rows of the rectangle from top to bottom with numbers from 1 to n, and the columns — from left to right with numbers from 1 to m. Let's denote the cell at the intersection of the r-th row and the c-th column as (r, c).

Initially the pig stands in cell (1, 1), and in the end she wants to be 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 each other, and some look very different. Peppa enjoys taking pictures and at every step she takes a picture of the cell where she is now. The path through the forest is considered to bebeautiful if photographs taken on her way, can be viewed in both forward and in reverse order, showing the same sequence 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 be very large, determine the remainder after dividing it by 109 + 7.

Input

The first line contains two integers n, m (1 ≤ n, m ≤ 500) — the height and width of the field.

Each of the following n lines contains m lowercase English letters identifying the types of cells of the forest. Identical cells are represented by identical letters, different cells are represented by different letters.

Output

Print a single integer — the number of beautiful paths modulo 109 + 7.

Examples
input
3 4
aaab
baaa
abba
output
3
Note

Picture illustrating possibilities for the sample test.

题意:

  N*M的字符矩阵,从(1,1)走到(N,M)有多少种方法能使路径上的字符串是回文串

题解:

  一个点走,相当于两个点分别从(1,1)向下向右走,另一个从(N,M)向上向左走,并且这两个点走的字符必须相同,dp[step][x1][y1][x2][y2]表示走step步,两个点分别到达(x1,y1),(x2,y2)这两个点并且路径上的字符相同的方案数有多少,那么每次按照他们能走的方向递推就行了。还有个问题这样的dp数组是开不下的,首先可以把它写成滚动数组,然后,因为知道起点,步数还有x坐标,y坐标是可以计算出来的,所以可以把y坐标的两维省掉。
于是就是枚举步数和两个x坐标了,还有点需要注意的就是N+M是奇数的时候

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int MOD=1e9+;
int dp[][maxn][maxn];
int N,M;
char s[maxn][maxn];
void add(int &x,int y){
x+=y;
if(x>=MOD)x-=MOD;
}
int main(){
scanf("%d%d",&N,&M);
for(int i=;i<=N;i++){
scanf("%s",s[i]+);
}
int cur=;
dp[][][N]=(s[][]==s[N][M]);
for(int step=;step<=(M+N-)/;step++){
cur^=;
for(int i=;i<=N;i++){
for(int j=;j<=N;j++){
dp[cur][i][j]=;
}
}
for(int x1=;x1<=N&&x1-<=step;x1++){
for(int x2=N;x2>=&&N-x2<=step;x2--){
int y1=+step-(x1-);
int y2=M-(step-(N-x2));
if(s[x1][y1]!=s[x2][y2])continue;
add(dp[cur][x1][x2],dp[cur^][x1][x2]);
add(dp[cur][x1][x2],dp[cur^][x1][x2+]);
add(dp[cur][x1][x2],dp[cur^][x1-][x2]);
add(dp[cur][x1][x2],dp[cur^][x1-][x2+]);
}
}
}
int ans=;
for(int i=;i<=N;i++){
add(ans,dp[cur][i][i]);
}
if((N+M)%){
for(int i=;i<N;i++){
add(ans,dp[cur][i][i+]);
}
}
printf("%d\n",ans);
return ;
}

Codeforces Round #316 (Div. 2)E. Pig and Palindromes DP的更多相关文章

  1. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  2. Codeforces Codeforces Round #316 (Div. 2) C. Replacement set

    C. Replacement Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/proble ...

  3. Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树

    C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...

  4. Codeforces Round #316 (Div. 2) C. Replacement

    题意:给定一个字符串,里面有各种小写字母和' . ' ,无论是什么字母,都是一样的,假设遇到' . . ' ,就要合并成一个' .',有m个询问,每次都在字符串某个位置上将原来的字符改成题目给的字符, ...

  5. Codeforces Round #316 (Div. 2) B. Simple Game

    思路:把n分成[1,n/2],[n/2+1,n],假设m在左区间.a=m+1,假设m在右区间,a=m-1.可是我居然忘了处理1,1这个特殊数据.被人hack了. 总结:下次一定要注意了,提交前一定要看 ...

  6. Codeforces Round #316 (Div. 2) D计算在一棵子树内某高度的节点

    题:https://codeforces.com/contest/570/problem/D 题意:给定一个以11为根的n个节点的树,每个点上有一个字母(a~z),每个点的深度定义为该节点到11号节点 ...

  7. Codeforces Round #316 (Div. 2) D. Tree Requests dfs序

    D. Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  8. Codeforces Round #316 (Div. 2)

    A. Elections time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  9. Codeforces Round #316 (Div. 2) D、E

    Problem D: 题意:给定一棵n个点树,每个点有一个字母,有m个询问,每次询问某个节点x的子树中所有深度为k的点能否组成一个回文串 分析:一堆点能组成回文串当且仅当数量为奇数的字母不多于1个,显 ...

随机推荐

  1. Java攻城狮之基础练习题------经典例题

    (一)键盘录入1----7,分别于控制台输出对应的周一,周二,周三,周四,周五,周六,周天. (二)设置一个数组,求出数组中对应的最大值以及索引. (三)在控制台输出9x9乘法口诀表. (四)使用冒泡 ...

  2. Svn install and use

    1.安装服务 使用yum安装subversion,简单.不繁琐. 1 yum install -y subversion 2.创建版本库 1 2       mkidr /svn/obj        ...

  3. cropper+pillow处理上传图片剪裁(一)

    在写新博客的时候,遇到需要用户上传自定义图片的处理,查了一番资料,决定用cropper和pillow来处理需要剪裁的图片上传,大致思路是:前端收集用户上传的图片和用户剪裁的尺寸数据,后台接收图片后按数 ...

  4. javascript中五句话

    1.弹出框 ,小括号中就是弹出的内容 alert("我是一个弹出框");   2.控制台输出 小括号里面就是 控制台输出的东西 console.log("我是控制台输出的 ...

  5. Java集合(一)HashMap

    HashMap 特点: HashMap的key和value都允许为空,无序的,且非线程安全的 数据结构: HashMap底层是一个数组,数组的每一项又都是链表,即数据和链表的结合体.当新建一个Hash ...

  6. Bin文件

    那什么是bin文件呢?为什么这么关键? bin (binary)既是:二进制, 里面存放的一般是可执行的二进制文件.二进制即是机器代码,汇编语言编译后的结果.我们编译的是高级语言,把高级语言翻译为机器 ...

  7. QT4.8界面设计(MSVC2010X)

    1.C++ IDE设计 MFC这种半死不活的windows C++平台已经被抛弃,很无奈.转向Qt的C++还是不错的选择. QT的QML才是最新的亮点,可惜没有时间经历这些东西了. 2.程序代码 2. ...

  8. Approximate Nearest Neighbors.接近最近邻搜索

    (一):次优最近邻:http://en.wikipedia.org/wiki/Nearest_neighbor_search 有少量修改:如有疑问,请看链接原文.....1.Survey:Neares ...

  9. SQL数据查询

    CREATE TABLE class0328( id INT, cname ), sex ), age INT, birthday DATE, html DOUBLE, js DOUBLE, scor ...

  10. JS 20180416考试

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...