完全不会这种类型的$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:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll; const int N = ;
const ll P = 1e9 + ; int n, m;
ll f[][N][N];
char mp[N][N]; template <typename T>
inline void inc(T &x, T y) {
x += y;
if(x >= P) x -= P;
} int main() {
scanf("%d%d", &n, &m);
/* scanf("%d", &n);
m = n; */
for(int i = ; i <= n; i++) scanf("%s", mp[i] + ); if(mp[][] != mp[n][m]) return puts(""), ; f[][][n] = 1LL;
for(int s = ; s <= (n + m) / ; ++s) {
int now = s & , pre = (s - ) & ;
memset(f[now], 0LL, sizeof(f[now])); for(int xa = ; xa <= s; ++xa)
for(int xb = n; xb >= n - s + ; --xb) {
int ya = s + - xa, yb = n + m + - s - xb;
if(xa > xb || ya > yb) continue;
if(mp[xa][ya] != mp[xb][yb]) continue;
inc(f[now][xa][xb], f[pre][xa][xb]);
inc(f[now][xa][xb], f[pre][xa - ][xb]);
inc(f[now][xa][xb], f[pre][xa][xb + ]);
inc(f[now][xa][xb], f[pre][xa - ][xb + ]);
}
} ll ans = 0LL; int cur = ((n + m) / ) & ;
if((n + m) % == ) {
for(int i = ; i <= n; i++) {
inc(ans, f[cur][i][i]);
inc(ans, f[cur][i][i + ]);
}
} else {
for(int i = ; i <= n; i++)
inc(ans, f[cur][i][i]);
} printf("%lld\n", ans);
return ;
}

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. hdoj-2141-Can you find it?(二分查找)

    题目链接 /* Name:HDU-2141-Can you find it? Copyright: Author: Date: 2018/4/12 17:15:46 Description: 暴力,复 ...

  2. codeforces D. Area of Two Circles' Intersection 计算几何

    D. Area of Two Circles' Intersection time limit per test 2 seconds memory limit per test 256 megabyt ...

  3. uva11997 K Smallest Sums&&UVALive 3135 Argus(优先队列,多路归并)

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...

  4. HihoCoder1403 后缀数组一·重复旋律1

    后缀数组一·重复旋律 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为长度为 N 的数构成的数列. 小Hi ...

  5. Oracl使用总结二

    1.ORA-00972: 标识符过长 错误排除 可能原因: 1.如果是拼接成的sql语句,请查找传递参数时字符型字段是否两边少了引号.2.数据库表名太长了,附各种类型的数据库表名长度: SQLSERV ...

  6. Mybatis相关SQL操作总结

    1.resultMap和resultType等参数以及结果集 <select id="getApplicationByRoleCode" resultType="p ...

  7. 在linux里建立一个快捷方式,连接到另一个目录

    ln -s 源目录 目标快捷方式比如要在/home/下面建立一个叫WIN7的快捷方式,指向/mnt/:ln -s /home/WIN7  /mnt

  8. 应用层-day01

    主流应用程序体系结构:CS结构.P2P结构. CS结构:客户-服务器体系结构.有一台总是打开的主机称为服务器,它服务来自其他许多称为客户的主机的请求. P2P体系结构:应用程序在不同的主机间链接,被称 ...

  9. boost_1.61.0编译安装

    1.下载源码boost_1_61_0.zip 2.进入目录 C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\Shor ...

  10. DATAX动态参数数据传递

    实例:ORACLE到ORACLE的数据传递   编写job.xml文件,添加变量参数 执行datax.py文件时记得带参数 格式:./datax.py –p"-Ddbname=*** -Di ...