【25.64%】【codeforces 570E】Pig and Palindromes
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的更多相关文章
- CodeForces 570E DP Pig and Palindromes
题意:给出一个n行m列的字符矩阵,从左上角走到右下角,每次只能往右或者往下走,求一共有多少种走法能得到回文串. 分析: 可以从两头开始考虑,每次只走一样字符的格子,这样得到的两个字符串拼起来之后就是一 ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- codeforces 570 E. Pig and Palindromes (DP)
题目链接: 570 E. Pig and Palindromes 题目描述: 有一个n*m的矩阵,每个小格子里面都有一个字母.Peppa the Pig想要从(1,1)到(n, m).因为Peppa ...
- 【25.33%】【codeforces 552D】Vanya and Triangles
time limit per test4 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- 【25.00%】【codeforces 584E】Anton and Ira
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【44.64%】【codeforces 743C】Vladik and fractions
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 510C】Fox And Names
[题目链接]:http://codeforces.com/contest/510/problem/C [题意] 给你n个字符串; 问你要怎么修改字典序; (即原本是a,b,c..z现在你可以修改每个字 ...
- 【codeforces 514E】Darth Vader and Tree
[题目链接]:http://codeforces.com/problemset/problem/514/E [题意] 无限节点的树; 每个节点都有n个儿子节点; 且每个节点与其第i个节点的距离都是ai ...
- 【codeforces 807B】T-Shirt Hunt
[题目链接]:http://codeforces.com/contest/807/problem/B [题意] 你在另外一场已经结束的比赛中有一个排名p; 然后你现在在进行另外一场比赛 然后你当前有一 ...
随机推荐
- Shiro学习总结(3)——Apache Shiro身份认证
身份验证,即在应用中谁能证明他就是他本人.一般提供如他们的身份ID一些标识信息来表明他就是他本人,如提供身份证,用户名/密码来证明. 在shiro中,用户需要提供principals (身份)和cre ...
- WEB安全实战(二)带你认识 XSS 攻击
前言 上一篇文章写了关于 WEB 安全方面的实战,主要是解决 SQL 盲注的安全漏洞.这篇文章本来是要写一篇关于怎样防治 XSS 攻击的,可是想来想去,还是决定先从理论上认识一下 XSS 吧.下一篇文 ...
- 使用knockout.js 完毕template binding
//1.template <script id="txn-details-template" type="text/html"> <!--St ...
- gogodroid--android 上的IPV6工具
gogodroid--android 上的IPV6工具 系统需求是 Android 1.6以上的系统,已经root,能够执行modprobe命令(在终端里输入modprobe,如果显示了帮助便可以), ...
- LinearLayout-layout_gravity 属性没有效果分析
今天在一个布局文件中,遇到了一个问题,先看代码 <LinearLayout android:layout_width="match_parent" android:layou ...
- Android学习笔记之Bitmap位图的旋转
位图的旋转也可以借助Matrix或者Canvas来实现. 通过postRotate方法设置旋转角度,然后用createBitmap方法创建一个经过旋转处理的Bitmap对象,最后用drawBitmap ...
- Fiddler代理配置
1.下载安装软件Fiddler 2.Fiddler设置HTTPS代理(如果代理的是https请求的需要操作这一步) 打开Fiddler,菜单栏:Tools -> Fiddler Options ...
- 12、python单步调试工具pdb
pdb 第4种方式是启动Python的调试器pdb,让程序以单步方式运行,可以随时查看运行状态.我们先准备好程序: # err.py s = '0' n = int(s) print(10 / n) ...
- sass和less,优秀的前端样式预处理器
身为切图界的一员,或者说在前端界打滚了一段日子的你.会慢慢地发现.如今的css编写已经不能满足自己的效率. 假设有更强大的框架,让你的css更灵活和更easy复用和维护,那该多好啊.非常明显,这个早已 ...
- 推荐一款稳定快速免费的前端开源项目 CDN 加速服务
前面学习到什么是CDN,全称是Content Delivery Network,即内容分发网络.CDN的通俗理解就是网站加速,CPU均衡负载. CDN的基本思路是尽可能避开互联网上有可能影响数据传输速 ...