题目链接:

  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. FDMemTable的详细使用方法

    unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Var ...

  2. hdu 4599 Dice

    数学能力已经彻底降低到了小学水平,真是惨啊惨... 首先G(M)很容易确定,G(M) = 6 * M; H(N) = 6 * F(N),于是推出来H(N)就可以了,昨天请教了一下别人,直接数学公式搞定 ...

  3. [TypeScript] Query Properties with keyof and Lookup Types in TypeScript

    The keyof operator produces a union type of all known, public property names of a given type. You ca ...

  4. RGB中的颜色的设置

    用来表示一个 RGB 颜色值. 语法 RGB(red, green, blue) RGB 函数的语法含有以下这些命名参数: 部分 描述 red 必要参数:Variant (Integer).数值范围从 ...

  5. LuaInterface简单介绍

    LuaInterface简单介绍 Lua是一种非常好的扩展性语言.Lua解释器被设计成一个非常easy嵌入到宿主程序的库.LuaInterface则用于实现Lua和CLR的混合编程. (一)Lua f ...

  6. 24Web前端架构

    近来都是接触前端.所以学多点这方面的东西,虽说有实战到项目里面去了,但可能还没走到所谓正确的道路上去.欢迎交流. 转载请说明来着:http://blog.csdn.net/wowkk -------- ...

  7. UVA 10288 - Coupons(概率递推)

    UVA 10288 - Coupons option=com_onlinejudge&Itemid=8&page=show_problem&category=482&p ...

  8. 机器学习和深度学习笔记(Matlab语言实现)

    不多说,直接上干货! 这里,对于想用matlab语言来做的朋友,强烈推荐 http://www.cnblogs.com/tornadomeet/

  9. Makefile详解 (转--不错就是有点长)

    概述 —— 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和 professional的程序员,make ...

  10. 20170212-备份ABAP程序

    把生产机上所有后续开发的CBO程序都备份下来.以备急用! 用过2种方法:1.写BDC程序,模拟 TCODE:SE38 -->Program --> Utilities(M)-->Mo ...