题目链接:

  570 E. Pig and Palindromes

题目描述:

  有一个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)的更多相关文章

  1. 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 ...

  2. 【25.64%】【codeforces 570E】Pig and Palindromes

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 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 ...

  4. Codeforces 570 - A/B/C/D/E - (Done)

    链接:https://codeforces.com/contest/570 A - Elections - [水] AC代码: #include<bits/stdc++.h> using ...

  5. CF 316div2 E.Pig and Palindromes

    E. Pig and Palindromes Peppa the Pig was walking and walked into the forest. What a strange coincide ...

  6. [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)

    [Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...

  7. [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】

    [CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...

  8. [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)

    [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...

  9. Codeforces 570E - Pig and Palindromes - [滚动优化DP]

    题目链接:https://codeforces.com/problemset/problem/570/E 题意: 给出 $n \times m$ 的网格,每一格上有一个小写字母,现在从 $(1,1)$ ...

随机推荐

  1. C# 读自己的资源文件

    Assembly assm = this.GetType().Assembly;//Assembly.LoadFrom(程序集路径); foreach (string resName in assm. ...

  2. 去掉小程序textarea上的完成按钮栏

    小程序textarea上会自动多一个完成按钮,如下图所示,如果是mpVue,在textarea添加     :show-confirm-bar="false"     即可.  

  3. [Angular] Expose Angular Component Logic Using State Reducers

    A component author has no way of knowing which state changes a consumer will want to override, but s ...

  4. ES文件浏览器 WIFI 查看电脑文件怎么弄

    1 开启来宾账户   2 右击要共享的文件夹,添加Guest共享(如果只是要查看共享的资源,权限级别为读取即可)   3 共享之后,网络路径就是"\\"+你的计算机名+" ...

  5. JS地区四级级联

    <script type="text/javascript" src="../js/jsAddress.js"></script> &l ...

  6. C++对象模型——解构语意学(第五章)

    5.4    对象的效率 (Object Efficiency) 在下面的效率測试中,对象构造和拷贝所须要的成本是以Point3d class声明为基准,从简单形式逐渐到复杂形式,包含Plain Ol ...

  7. 谁是性能杀手?Kafka多Topic下启用SSL时延增大问题分析

    问题背景 项目中将Kafka接口进行RESTful封装,在使用RESTful接口进行性能测试时,发现Topic数增多后,开启SSL与非SSL进行测试,发现开启SSL后性能下降得厉害.例如600个Top ...

  8. MVC为Html对象建立一个扩展方法,使用自己的控件就像使用TextBox一样方便

    先看一下我想要的结果: 很容易它就是一个单选按钮组,当我后台为Html对象(HtmlHelper的一个实例,它被定义在System.Web.Mvc名称空间下的WebViewPage类,即它对于所有MV ...

  9. java8--多线程(java疯狂讲义3复习笔记)

    多线程这块,平时用的框架里都封装好了,只有写批处理和工具包时用过几次.现在水平仅仅限于会用的程度,需要全面深入学习多线程. 主要内容:创建线程,启动线程,控制线程,多线程的同步,线程池,使用线程安全的 ...

  10. make eval builtin function

    1 eval的返回值是空字符串,因此它可以用于Makefile的任何位置而不引起错误 2 eval函数的作用效果 生成Makefile的动态部分,即eval用于增加Makefile的构成部分. 也就是 ...