time limit per test4 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

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 be beautiful 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.





【题目链接】:http://codeforces.com/contest/570/problem/E

【题解】



可以看成是两个人在走路;

一个人(x1,y1)从左上角往右下角走;

另外一个人(x2,y2)从右下角往左上角走;

则设f[step][x1][y1][x2][y2]表示两个人都走了step步,第一个人到了x1,y1第二个人到了x2,y2的方案数;

(要时刻满足x1<=x2且y1<=y2);

则f[step][x1][y1][x2][y2]=f[step-1][x1-1][y1][x2+1][y2]…..

但是这样搞空间复杂度太大;

注意到,如果step确定了,则根据x1,x2我们完全可以直接得到y1和y2;

因为

y1+x1-1=step

n-x2+m-y2+1=step;

则我们可以减小f数组的维数

变成

f[step][x1][x2];

(这样枚举step最后要变成(n+m)/2,它们最后才会相遇或相邻);

(要时刻满足x1<=x2且y1<=y2);

而f[step]只与f[step-1]有关;

所以考虑用滚动数组;

转移方程也变成

f[now][x1][x2] = f[now^1][x1][x2]+f[now^1][x1-1][x2]+f[now^1][x1-1][x2+1]+f[now^1][x1][x2+1];

最后在f[1..n][i][i]中找最大值;

但如果n+m为奇数;

则它们最后到的不是同一格

可能是相邻的上下两个或左右两格;(只有n+m为偶数才会到同一格);





【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int MAXN = 550;
const int MOD = 1e9+7;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0); int n,m,now = 0;
char s[MAXN][MAXN];
int f[2][MAXN][MAXN]; void add(int &a,int &b)
{
a = a+b;
if (a>=MOD) a -= MOD;
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);rei(m);
rep1(i,1,n)
scanf("%s",s[i]+1);
if (s[1][1]!=s[n][m])
{
puts("0");
return 0;
}
f[now][1][n] = 1;
rep1(step,2,(n+m)/2)
{
now^=1;
memset(f[now],0,sizeof f[now]);
rep1(x1,1,step)
rep2(x2,n,n-step+1)
{
int y1 = step+1-x1,y2 = n-x2+1+m-step;
if (y1>y2 || x1>x2)
continue;
if (s[x1][y1]==s[x2][y2])
{
add(f[now][x1][x2],f[now^1][x1][x2]);
add(f[now][x1][x2],f[now^1][x1-1][x2]);
add(f[now][x1][x2],f[now^1][x1-1][x2+1]);
add(f[now][x1][x2],f[now^1][x1][x2+1]);
}
}
}
int ans = 0;
if ((n+m)&1)
{
rep1(i,1,n)
add(ans,f[now][i][i]),add(ans,f[now][i][i+1]);
}
else
rep1(i,1,n)
add(ans,f[now][i][i]);
printf("%d\n",ans);
return 0;
}

【25.64%】【codeforces 570E】Pig and Palindromes的更多相关文章

  1. CodeForces 570E DP Pig and Palindromes

    题意:给出一个n行m列的字符矩阵,从左上角走到右下角,每次只能往右或者往下走,求一共有多少种走法能得到回文串. 分析: 可以从两头开始考虑,每次只走一样字符的格子,这样得到的两个字符串拼起来之后就是一 ...

  2. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  3. codeforces 570 E. Pig and Palindromes (DP)

    题目链接: 570 E. Pig and Palindromes 题目描述: 有一个n*m的矩阵,每个小格子里面都有一个字母.Peppa the Pig想要从(1,1)到(n, m).因为Peppa ...

  4. 【25.33%】【codeforces 552D】Vanya and Triangles

    time limit per test4 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...

  5. 【25.00%】【codeforces 584E】Anton and Ira

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【44.64%】【codeforces 743C】Vladik and fractions

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【codeforces 510C】Fox And Names

    [题目链接]:http://codeforces.com/contest/510/problem/C [题意] 给你n个字符串; 问你要怎么修改字典序; (即原本是a,b,c..z现在你可以修改每个字 ...

  8. 【codeforces 514E】Darth Vader and Tree

    [题目链接]:http://codeforces.com/problemset/problem/514/E [题意] 无限节点的树; 每个节点都有n个儿子节点; 且每个节点与其第i个节点的距离都是ai ...

  9. 【codeforces 807B】T-Shirt Hunt

    [题目链接]:http://codeforces.com/contest/807/problem/B [题意] 你在另外一场已经结束的比赛中有一个排名p; 然后你现在在进行另外一场比赛 然后你当前有一 ...

随机推荐

  1. jquery--this

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  2. vmware-虚拟机播放器的下载、安装

    如果是在window下安装的话: 1.下载vmware: 到官网下载免费个人版本 https://my.vmware.com/cn/web/vmware/free#desktop_end_user_c ...

  3. checkbox-padding 调整checkbox字体跟图标距离

    有时候我们会遇到需要调整控件中的内容相对于容器的位置.这里有两种情况 1.linearlayout这样的容器中,包含button类的控件,这时候margin可以调节 2.textview中的文字内容 ...

  4. 洛谷 P2562 [AHOI2002]Kitty猫基因编码

    P2562 [AHOI2002]Kitty猫基因编码 题目描述 小可可选修了基础生物基因学.教授告诉大家 Super Samuel 星球上 Kitty猫的基因的长度都是 2 的正整数次幂 ), 全是由 ...

  5. Codeforces 559A Gerald&#39;s Hexagon 数三角形

    题意:按顺序给出一个各内角均为120°的六边形的六条边长,求该六边形能分解成多少个边长为1的单位三角形. 把单位三角形面积看做1,实际上就是求六边形面积.随便找六边形的三条互相不相邻的边,分别以这三条 ...

  6. 移动mm 话费支付接入过程(ane)

    下面记录移动mm 话费支付接入的过程 1.强联网.弱联网差别.sdk是否有区分?用户体验部分由什么不同和差异? 差别在于强联网是网络通道(wifi/gprs/3g),弱联网是走短信通道,用户层面差异在 ...

  7. 用电脑从Google Play下载apk

    用电脑从Google Play下载apk 方法一:给Chrome浏览器安装apk-downloader插件,需禁止 SSL 错误警告,即在Chrome的快捷方式上加入"--ignore-ce ...

  8. Android中Alarm的机制

    本次给大家分析的是Android中Alarm的机制所用源码为最新的Android4.4.4.首先简单介绍如何使用Alarm并给出其工作原理,接着分析Alarm和Timer以及Handler在完成定时任 ...

  9. 软件——python,主函数

    1;; 如何在spyder中运行python程序 如下图,   写入一个输出  ' hellow word '的程序 然后点击运行按钮就可以运行了.

  10. 最新GitHub新手使用教程(Windows Git从安装到使用)——详细图解

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 一.叙述 1.Git简介 Git(读音为/gɪt/.)是一个开源的分布式版本控制系统,可以有效.高速地处理从很小到非常大的项目版本 ...