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)$ ...
随机推荐
- C++设计模式之适配器模式(二)
3.Socket网络通信的设计与实现------类适配器 除了对象适配器模式之外.适配器模式另一种形式.那就是类适配器模式,类适配器模式和对象适配器模式最大的差别在于适配器和适配者之间的关系不同,对象 ...
- Leetcode:search_insert_position
一. 题目 给定一个数组和要插入数的大小.求插入的位置. 二. 分析 太水,直接扫描.过--. class Solution { public: int searchInsert(in ...
- [原创+分享]Mandelbrot Explorer
Mandelbrot Explorer 是一款用于在MandelBort集/Julia集上进行无限漫游的软件,使用VS2013+CUDA6.5开发而成.它也是我学习CUDA开发的一个小小的成果,欢迎大 ...
- PAT-PAT (Advanced Level) Practise 1001. A+B Format (20) 【二星级】
题目链接:http://www.patest.cn/contests/pat-a-practise/1001 题面: 1001. A+B Format (20) Calculate a + b and ...
- WWDC笔记:2013 Session 203 What’s New in Cocoa Touch(未完)
Multitasking Background fetching New background mode fetch - (void)application:(UIApplication *)appl ...
- iOS7获取UUID以及转换MD5
近期项目开发,运用到要获取UUID转MD5,可是iOS7不能使用获取的UDID的接口(涉及到隐私),获取MAC地址的方式的接口在iOS7下也废弃了.眼下可能的就是获取UUID了,可是在iOS7下,UU ...
- ranlib
1 ranlib的缩写 random access library 2 ranlib的作用 为静态库的符号建立索引,可以加速链接,因此称用ranlib处理过的library为random access ...
- bzoj2461: [BeiJing2011]符环
再做水题就没救了 考虑DP...我们把正反面一起弄 fi,j,k,u表示第几个位置,正面多了多少左括号,背面多了多少没办法消的右括号,背面结尾的左括号数 #include<cstdio> ...
- ubuntu php5.6源码安装
本系列的lnmp的大框架基本上是按照http://www.linuxzen.com/lnmphuan-jing-da-jian-wan-quan-shou-ce-si-lnmpda-jian-yuan ...
- 使用C#开发HTTP服务器系列之访问主页
各位朋友大家好,我是秦元培,欢迎大家关注我的博客,我的博客地址是http://qinyuanpei.com.在这个系列文章的第一篇中,我们着重认识和了解了HTTP协议,并在此基础上实现了一个可交互的W ...