codeforces 570 E. Pig and Palindromes (DP)
题目链接:
题目描述:
有一个n*m的矩阵,每个小格子里面都有一个字母。Peppa the Pig想要从(1,1)到(n, m)。因为Peppa the Pig是一个完美主义者,她想要她所经过的路径上的字母组成的字符串是一个回文串,现在Peppa the Pig想要知道有多少满足条件的走法?
解题思路:
因为经过路径上的字母要组成回文串,所以可以从(1,1),(n,m)同时开始dp。从(1,1)出发只能向下方和右方走,从(n,m)出发只能向上方和左方走。然后就可以dp[x1][y1][x2][y2],其实呢可以把dp数组优化到三维dp[step][x1][x2]。因为从两点出发走过的步数是一样的,知道了走过的步数和一个方向的坐标,就可以求出另一个方向的坐标咯。但是酱紫搞的话,还是会MTL的(亲身经历>_<)······,但是请我们尊贵的滚动数组出场就一切ok咯。
#include <bits/stdc++.h>
using namespace std; const int maxn = ;
const int mod = ;
int dp[][maxn][maxn], n ,m;
char maps[maxn][maxn]; int main ()
{
while (scanf ("%d %d", &n, &m) != EOF)
{
for (int i=; i<=n; i++)
scanf ("%s", maps[i]+); if (maps[][] != maps[n][m])
{
printf ("0\n");
continue;
} memset (dp, , sizeof(dp));
dp[][][n] = ;
int res = (n + m - ) / ; for (int i=; i<=res; i++)
{
for (int j=; j<=i+; j++)
for (int k=n; k>=n-i; k--)
{
int x1, x2, y1, y2;
x1 = j, y1 = i - j + ;
x2 = k, y2 = n + m - k - i; if (maps[x1][y1] == maps[x2][y2])
{
if (x1>x2 || y1>y2)
continue;
dp[i%][j][k] = (dp[i%][j][k] + dp[(i%)^][j-][k])%mod;
dp[i%][j][k] = (dp[i%][j][k] + dp[(i%)^][j-][k+])%mod;
dp[i%][j][k] = (dp[i%][j][k] + dp[(i%)^][j][k])%mod;
dp[i%][j][k] = (dp[i%][j][k] + dp[(i%)^][j][k+])%mod;
} }
memset(dp[(i%)^], , sizeof(dp[(i%)^]));
} int ans = ;
if ((n+m)% == )
{
for (int i=; i<=n; i++)
ans = (ans + dp[res%][i][i]) % mod;
}
else
for (int i=; i<=n; i++)
{
int x = res - i + ;
if (x + <= m)
ans = (ans + dp[res%][i][i]) % mod;
if (i + <= n)
ans = (ans + dp[res%][i][i+]) % mod;
}
printf ("%d\n", ans); }
return ;
}
codeforces 570 E. Pig and Palindromes (DP)的更多相关文章
- Codeforces Round #316 (Div. 2)E. Pig and Palindromes DP
E. Pig and Palindromes Peppa the Pig was walking and walked into the forest. What a strange coinci ...
- 【25.64%】【codeforces 570E】Pig and Palindromes
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- D Tree Requests dfs+二分 D Pig and Palindromes -dp
D time limit per test 2 seconds memory limit per test 256 megabytes input standard input output stan ...
- Codeforces 570 - A/B/C/D/E - (Done)
链接:https://codeforces.com/contest/570 A - Elections - [水] AC代码: #include<bits/stdc++.h> using ...
- CF 316div2 E.Pig and Palindromes
E. Pig and Palindromes Peppa the Pig was walking and walked into the forest. What a strange coincide ...
- [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)
[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...
- [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】
[CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...
- [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)
[Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...
- Codeforces 570E - Pig and Palindromes - [滚动优化DP]
题目链接:https://codeforces.com/problemset/problem/570/E 题意: 给出 $n \times m$ 的网格,每一格上有一个小写字母,现在从 $(1,1)$ ...
随机推荐
- java去空格
1.trim()是去掉首尾空格 2.str.replace(" ", ""); 去掉所有空格,包括首尾.中间 3.或者replaceAll(&quo ...
- linux下启动mysql服务(类似于windows下net start mysql)
1.linux系统启动方式:service mysql start.其类似于windows下net start mysql
- OCR简介及使用
OCR (Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗.亮的模式确定其形状,然后用字符识别方法将形状翻译 ...
- 《Spring设计思想》AOP设计基本原理
0.前言 Spring 提供了AOP(Aspect Oriented Programming) 的支持, 那么,什么是AOP呢?本文将通过一个另外一个角度来诠释AOP的概念,帮助你更好地理解和使用Sp ...
- 【OpenGL】Shader实例分析(七)- 雪花飘落效果
转发请保持地址:http://blog.csdn.net/stalendp/article/details/40624603 研究了一个雪花飘落效果.感觉挺不错的.分享给大家,效果例如以下: 代码例如 ...
- Random Forest 与 GBDT 的异同
曾经在看用RF和GBDT的时候,以为是非常相似的两个算法,都是属于集成算法,可是细致研究之后,发现他们根本全然不同. 以下总结基本的一些不同点 Random Forest: bagging (你懂得. ...
- CSS多种方法实现分隔线
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8" /> <title&g ...
- java 配置时遇到的问题及解决办法
1. 最近JDK更新很频繁,以至于我安装时版本太多,选择也会出现问题 首先,确定你选择的是32位版本还是64位版本(貌似64位系统下也可以安装32位的JDK), 这个相当重要,因为这个会影响到ecli ...
- Linux Shell_test
test: 测试Shell脚本里的条件,通过推出状态返回其结果.用法: test [ expression ] 或 [ [ expression ] ] 注意空格test表达式:是则为真 ...
- Android Studio keymap到Eclipse后,查找下一个同样变量快捷键Ctrl+K失效
注:升级到0.8的版本号以后.这个快捷键能够使了,只是另一个bug,假设你用了Ctrl+F先去查找了其它的东东,再使这个快捷键去定位另外一个变量可能偶尔会不灵,不灵的话还是能够用我以下的方式来让Ctr ...