完全不会这种类型的$dp$啊……

考虑回文串一定是可以拆分成(偶数个字母 + 偶数个字母)或者(偶数个字母 + 一个字母 +偶数个字母),两边的偶数个字母其实是完全对称的。因为这道题回文串的长度是给定的$n + m$,所以回文串的类型也是确定的。

发现直接$dp$不好转移,我们可以把走的步数拆成两半,从$(1, 1)$开始走$(n + m) / 2$步,从$(n, m)$开始走$(n + m) / 2$步,然后在中间相遇就可以计算答案,这样子只要每一次走到相同的格子就可以转移了。

我们先设计出一个暴力的状态就是$f_{stp, xa, ya, xb, yb}$表示走了$stp$步,从$(1, 1)$开始走到$(xa, ya)$,从$(n, m)$开始走到$(xb, yb)$的方案数。

显然空间炸了。

观察一下发现了$stp$这一维可以滚掉,而当$stp$确定时,只要知道了$xa$和$xb$就可以计算出$ya$和$yb$,具体计算过程可以自己$yy$一下。

最后统计答案的时候要注意讨论$(n + m)$的奇偶性。

时间复杂度$O(n^3)$。

Code:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5. typedef long long ll;
  6.  
  7. const int N = ;
  8. const ll P = 1e9 + ;
  9.  
  10. int n, m;
  11. ll f[][N][N];
  12. char mp[N][N];
  13.  
  14. template <typename T>
  15. inline void inc(T &x, T y) {
  16. x += y;
  17. if(x >= P) x -= P;
  18. }
  19.  
  20. int main() {
  21. scanf("%d%d", &n, &m);
  22. /* scanf("%d", &n);
  23. m = n; */
  24. for(int i = ; i <= n; i++) scanf("%s", mp[i] + );
  25.  
  26. if(mp[][] != mp[n][m]) return puts(""), ;
  27.  
  28. f[][][n] = 1LL;
  29. for(int s = ; s <= (n + m) / ; ++s) {
  30. int now = s & , pre = (s - ) & ;
  31. memset(f[now], 0LL, sizeof(f[now]));
  32.  
  33. for(int xa = ; xa <= s; ++xa)
  34. for(int xb = n; xb >= n - s + ; --xb) {
  35. int ya = s + - xa, yb = n + m + - s - xb;
  36. if(xa > xb || ya > yb) continue;
  37. if(mp[xa][ya] != mp[xb][yb]) continue;
  38. inc(f[now][xa][xb], f[pre][xa][xb]);
  39. inc(f[now][xa][xb], f[pre][xa - ][xb]);
  40. inc(f[now][xa][xb], f[pre][xa][xb + ]);
  41. inc(f[now][xa][xb], f[pre][xa - ][xb + ]);
  42. }
  43. }
  44.  
  45. ll ans = 0LL; int cur = ((n + m) / ) & ;
  46. if((n + m) % == ) {
  47. for(int i = ; i <= n; i++) {
  48. inc(ans, f[cur][i][i]);
  49. inc(ans, f[cur][i][i + ]);
  50. }
  51. } else {
  52. for(int i = ; i <= n; i++)
  53. inc(ans, f[cur][i][i]);
  54. }
  55.  
  56. printf("%lld\n", ans);
  57. return ;
  58. }

CF570E Pig and Palindromes的更多相关文章

  1. CF 316div2 E.Pig and Palindromes

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

  2. codeforces 570 E. Pig and Palindromes (DP)

    题目链接: 570 E. Pig and Palindromes 题目描述: 有一个n*m的矩阵,每个小格子里面都有一个字母.Peppa the Pig想要从(1,1)到(n, m).因为Peppa ...

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

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

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

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

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

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

  7. CodeForces 570E DP Pig and Palindromes

    题意:给出一个n行m列的字符矩阵,从左上角走到右下角,每次只能往右或者往下走,求一共有多少种走法能得到回文串. 分析: 可以从两头开始考虑,每次只走一样字符的格子,这样得到的两个字符串拼起来之后就是一 ...

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

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

  9. Hadoop学习笔记—16.Pig框架学习

    一.关于Pig:别以为猪不能干活 1.1 Pig的简介 Pig是一个基于Hadoop的大规模数据分析平台,它提供的SQL-LIKE语言叫Pig Latin,该语言的编译器会把类SQL的数据分析请求转换 ...

随机推荐

  1. memcached使用libevent 和 多线程模式

    一.libevent的使用 首先我们知道,memcached是使用了iblievet作为网络框架的,而iblievet又是单线程模型的基于linux下epoll事件的异步模型.因此,其基本的思想就是 ...

  2. 数据清洗记录,pandas

    pandas数据清洗:http://www.it165.net/pro/html/201405/14269.html data=pd.Series([1,2,3,4]) data.replace([1 ...

  3. LeetCode K-diff Pairs in an Array

    原题链接在这里:https://leetcode.com/problems/k-diff-pairs-in-an-array/#/description 题目: Given an array of i ...

  4. Bootstrap确定样式让屏幕缩小后布局不乱

    解决方案是如下 结果如下:

  5. Tomcat 工作原理 1 (转)

    Tomcat 系统架构与设计模式,第 1 部分: 工作原理 这个分为两个部分的系列文章将研究 Apache Tomcat 的系统架构以及其运用的很多经典设计模式.本文是第 1 部分,将主要从 Tomc ...

  6. Message类的属性Msg所关联的消息ID

    在做C#的Message消息处理的时候,用到了消息的msg编号不知道对应的是什么事件,所以才从网上找来资料如下,在文章最后我会给出资料的出处的. WM_NULL=0x0000 WM_CREATE=0x ...

  7. CF gym 101933 K King's Colors —— 二项式反演

    题目:http://codeforces.com/gym/101933/problem/K 其实每个点的颜色只要和父亲不一样即可: 所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \) ...

  8. git公钥生成以及与coding等联合

    更好用的 https://segmentfault.com/a/1190000002645623 其中 $ ssh -T git@github.comgitbub $ ssh -T git@git.c ...

  9. maven工程编译成jar包

    在pom文件的project节点下增加build节点,mvn package即可 <build> <plugins> <plugin> <artifactId ...

  10. Poj 2395 Out of Hay( 最小生成树 )

    题意:求最小生成树中最大的一条边. 分析:求最小生成树,可用Prim和Kruskal算法.一般稀疏图用Kruskal比较适合,稠密图用Prim.由于Kruskal的思想是把非连通的N个顶点用最小的代价 ...