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)$ ...
随机推荐
- Linux面试题完整修订附加答案
册一: 1.Linux挂载Winodws共享文件夹 第一步:先在Windows上创建一个共享目录 Windows系统IP是172.16.18.56;共享文件夹:E:\test ...
- 【网络】TCP协议
一.概述 主要特点: 1)面向连接的运输层协议 2)每一条TCP连接只能有两个端点,每一条TCP连接只能是点对点的(一对一) 3)TCP提供可靠交付的服务 4)TCP提供全双工通信 5)面向字节流:T ...
- 简述HashMap和Hashtable的差别
1.HashMap继承AbstractMap类. Hashtable继承了Dictionary类. 2.HashMap同意有null的键和值. Hashtable不同意有null的键和值. ...
- 怎样更好的设计你的REST API之基于REST架构的Web Service设计及REST框架实现
一.REST 含状态传输(英文:Representational State Transfer,简称REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格. 眼下在 ...
- Android Studio keymap到Eclipse后,查找下一个同样变量快捷键Ctrl+K失效
注:升级到0.8的版本号以后.这个快捷键能够使了,只是另一个bug,假设你用了Ctrl+F先去查找了其它的东东,再使这个快捷键去定位另外一个变量可能偶尔会不灵,不灵的话还是能够用我以下的方式来让Ctr ...
- Flex+Java+Blazeds
1.环境:jdk1.6,Flex4.6 2.工具:MyEclipse10 3.server:Tomcat7 4.连接方式:Blazeds 5.项目类型:Flex项目 6.步骤 (1)新建Flex项目一 ...
- oracle 10g 实例用localhost无法访问的处理
我在笔记本上安装了一个Oracle10g,安装好了之后,查看E:\oracle\product\10.2.0\db_1\network\ADMIN\tnsnames.ora文件,发现SID对应的IP地 ...
- SQLServer导出单表数据
采用生成脚本---仅数据.. 如果是部分数据,可以先把部分数据备份到一个表中 select * into .. from ...
- 【bzoj2462】[BeiJing2011]矩阵模板
#include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> # ...
- git 团队代码管理交流共同进步
Installation methods for GitLab | GitLab https://about.gitlab.com/installation/#centos-7 gittutorial ...