【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; 然后你现在在进行另外一场比赛 然后你当前有一 ...
随机推荐
- IIS6下AD域设置
简介:IIS6下AD域设置 IIS6下AD域设置 http://files.cnblogs.com/files/KingUp/AD%E5%9F%9F%E8%AE%BE%E7%BD%AE.rar
- 【问题】VUE 同一页面路由参数变化,数据不刷新
依赖路由的params参数获取写在created生命周期里面,因为相同路由二次甚至多次加载的关系 没有达到监听,退出页面再进入另一个页面并不会运行created组件生命周期,导致数据还是第一次进入的数 ...
- 微信小程序简单常见首页demo
wxml <view class='index-contier'> <view class="index-left"> <view>电池剩余&l ...
- STM32 之ADC单次转换模式和连续转换模式
一.背景 在STM32中的AD的单通道采样中可以设置成单次转换模式和连续转换模式,如何理解这两个转换模式的区别,通过程序又是怎样实现的? 二.正文 首先理解单次转换模式,即ADC进行单次转换(单样本) ...
- 【2017"百度之星"程序设计大赛 - 初赛(B)】Chess
[链接]http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=776&pid=1001 [题意] 在这里写题意 [题 ...
- 洛谷 P1327 数列排序
P1327 数列排序 题目描述 给定一个数列{an},这个数列满足ai≠aj(i≠j),现在要求你把这个数列从小到大排序,每次允许你交换其中任意一对数,请问最少需要几次交换? 输入输出格式 输入格式: ...
- ontouch、dispatchtouchevent、interceptouchevent-相关事件
这几天一直在研究onTouch的相关方法,今天我们就来看看onTouchEvent.dispatchTouchEvent.onIntercepTouchEvent这三个方法在控件之间的传递顺序 pub ...
- loadrunner监控apache服务
一.apache配置步骤(假设apache服务已安装) 1.使用find / -name httpd.conf命令查找httpd.conf文件 2.使用cd opt/lampp/apache2/con ...
- Windows下编译OpenSSL(使用VS2013)
简述 OpenSSL是一个开源的第三方库,它实现了SSL(Secure SocketLayer)和TLS(Transport Layer Security)协议,被广泛企业应用所采用.对于一般的开发人 ...
- 通过Rman catalog 创建及管理Oracle数据库备份
基本环境信息target DB (需备份数据库) 192.168.199.67 ORACLE_SID=zgw HOSTNAME=Oracle11 catlog DB (备份管理数据库) 192.168 ...