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. C Linux read write function extension

    前言 - 赠送 readn / writen Linux 上默认的 read 和 write 函数会被信号软中断. 且 read 和 write 函数中第三个参数 count #include < ...

  2. Hashmap 详解和迭代器问题

    重点介绍HashMap.首先介绍一下什么是Map.在数组中我们是通过数组下标来对其内容索引的,而在Map中我们通过对象来对对象进行索引,用来索引的对象叫做key,其对应的对象叫做value.在下文中会 ...

  3. Service不完全解析

    本篇的内容并不是介绍service使用方法和生命周期的,而是对其中的一些要点进行记录和分析. 我们都知道,Service是一个在后台执行的应用组件,用于在后台进行长期操作,例如进行网络事务,播放背景音 ...

  4. Vue报错笔记

    1.错误信息:[Vue warn]: Property or method "object" is not defined on the instance but referenc ...

  5. sites for debugging script

    1. javascript debugger online: http://jsfiddle.net/ 2. script(js,python,ruby) debugger online: http: ...

  6. 组装自己的tesla超级计算机

    原文链接:blog.csdn.net/xqj198404/article/details/20016279 NVIDIA链接:http://www.nvidia.cn/object/tesla_bui ...

  7. ES2015 模板字符串 ``

    js中类似`${xx,yy}`的语句是什么意思? `string` 是模板字符串,ES2015新增的符号. var x = 'a', y = 'b'; var z = `${x,y}`; //'b' ...

  8. Php+Redis队列原理

    我们新建一个文件queue.php <?php while(true){ echo 1; sleep(1); } 然后中 命令行里面 执行 php queue 你会发现每秒钟输出一个1:等了很久 ...

  9. sublim Text3 配置python3环境

    一.安装Sublime Text 3 1.双击下载的.exe文件安装,安装路径不要有中文目录 2.安装Sublime Text 3时,勾选“Add to explorer context menu”, ...

  10. JavaScript对原始数据类型的拆装箱操作

    JS中的基本类型: boolean null undefined string number symbol 每当读取一个基本类型的时候,会隐式的自动进行装箱操作(即:将一个基本类型变成对象): var ...