完全不会这种类型的$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. angular复选框式js树形菜单(二)

    删除(过滤)树形结构某一个子节点: function filterTreeData(treeData){ angular.forEach(treeData,function(item){ if (it ...

  2. Shell编程-运算符

    1.declare命令 declare声明变量类型:declare [+/-][选项] 变量名 -:给变量设定类型属性 +:取消变量的类型属性 -a:将变量声明为数组型 -i:整数型 -x:环境变量 ...

  3. 201621123014《Java程序设计》第十周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 1. 常用异常 结合题集题目7-1回答 1.1 自己以前编写的代码中经常出现 ...

  4. L124

    I have a toothache because there is a cavity in one of my teeth. I founded an orphanage last year an ...

  5. ios 加密解密(包括base64,DES)非原创

    .h文件 #import <Foundation/Foundation.h> /******字符串转base64(包括DES加密)******/ #define __BASE64( tex ...

  6. CodeForces - 438D: The Child and Sequence(势能线段树)

    At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at ...

  7. CodeForces - 896D :Nephren Runs a Cinema(卡特兰数&组合数学---比较综合的一道题)

    Lakhesh loves to make movies, so Nephren helps her run a cinema. We may call it No. 68 Cinema. Howev ...

  8. 2825 codevs危险的组合(递推)

    2825 危险的组合 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 钻石 Diamond 题目描述 Description 有一些装有铀(用U表示)和铅(用L表示)的盒子,数量均足够 ...

  9. git revert reset

    git revert是用一次新的commit来回滚之前的commit,git reset是直接删除指定的commit. git reset 是把HEAD向后移动了一下,而git revert是HEAD ...

  10. url出现特殊字符,需要进行编码

    1) 网络访问请求:中文空格字符编码/解码 stringByAddingPercentEscapesUsingEncoding(只对 `#%^{}[]|\"<> 加空格共14个字 ...