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. POJ3087 Shuffle'm Up

    题目: 现有字符串s1.s2.s12,其中s1.s2的长度为len,s12的长度为2*len. 是否可以通过一些操作使s1和s2转换合并成s12? 变换的操作规则如下: 假设s1=11111,s2=0 ...

  2. ansible upload

    # 链接地址:https://www.cnblogs.com/xiaoxiaoleo/p/6626299.html # synchronize: 从拉取远程服务器文件,需要加mode: pull # ...

  3. python爬虫:读取PDF

    下面的代码可以实现用python读取PDF,包括读取本地和网络上的PDF. pdfminer下载地址:https://pypi.python.org/packages/source/p/pdfmine ...

  4. 如何像Uber一样给工程师派单 解放外包落后的生产力

    2014年,陈柯好的第一个创业项目失败,半年之内,陈柯好以技术合伙人的方式游走于旅游.电商.团购.票务等各种领域.正当他对职业方向感到迷茫时,“大众创业.万众创新”的口号被提了出来 一时间,技术需求被 ...

  5. (转)RabbitMQ学习之消息可靠性及特性

    http://blog.csdn.net/zhu_tianwei/article/details/53971296 下面主要从队列.消息发送.消息接收方面了解消息传递过的一些可靠性处理. 1.队列 消 ...

  6. span可编辑 属性 html 可编辑td

    <span contenteditable="true">11111111111111111</span> <!DOCTYPE html PUBLIC ...

  7. ZBrush细说3D海盗角色的创建艺术

    一提到海盗,就不由自主想到了<加勒比海盗>,那个帅得一塌糊涂的杰克船长更是让人夜不能寐寝难安,但在艺术的世界里,角色无美丑,今天我们要讲的这位海盗,就与“帅气”八竿子打不着了,它甚至有点古 ...

  8. Asp.net Core 源码-SessionExtensions

    using Microsoft.AspNetCore.Http; using Newtonsoft.Json; namespace SportsStore.Infrastructure { publi ...

  9. 常用css和js内容

    1.让一个200x200的div在不同分辨率屏幕上下左右居中. <div class="box"></div> <style type="t ...

  10. 树(2)-----leetcode(层、深度、节点)

    1.树的类实现: class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = N ...