题意:给出一个n行m列的字符矩阵,从左上角走到右下角,每次只能往右或者往下走,求一共有多少种走法能得到回文串。

分析:

可以从两头开始考虑,每次只走一样字符的格子,这样得到的两个字符串拼起来之后就是一个回文串。

设d(step, x1, y2, x2, y2)表示从左上角(1, 1)z往右下走step个格子走到(x1, y1),同时从右下角(n, m)往左上走step个格子走到(x2, y2),而且两边得到一样字符串的方法数。

一开始判断一下左上角和右下角的字符是不是一样,若一样,d(1, 1, 1, n, m) = 1,若不一样则直接输出0.

状态转移:

如果s[x1][y2] = s[x2][y2],而且x1 ≤ y1 && y1 ≤ y2

d(step, x1, y1, x2, y2) = d(step - 1, x1 - 1, y1, x2 + 1, y2) + d(step - 1, x1, y1 - 1, x2 + 1, y2) + d(step - 1, x1 - 1, y1, x2, y2 + 1) + d(step - 1, x1, y1- 1, x2, y2+ 1)

这是一个五维的状态空间太大了,空间优化:

首先如果已知step的话,x1和y1只要知道其中一个,就可以计算出另一个,x2和y2同理,所以我们可以把y1和y2去掉,这样把五维优化成三维。

另外一个优化就是,我们在递推的时候是按照step从小到大递推的,所以可以用滚动数组来优化,这样空间复杂度为O(n^2)。

另外要考虑一下奇偶的问题:

如果整个字符串长度为奇数的话,两个字符串会相遇在同一个格子

如果整个字符串长度为偶数的话,两个字符串会走到相邻的格子,要么是同一行左右相邻,要么是同一列上下相邻

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; typedef long long LL; const int maxn = + ;
const int MOD = ; int d[][maxn][maxn]; char G[maxn][maxn]; void add(int& a, int b) { a += b; if(a >= MOD) a -= MOD; } int main()
{
//freopen("in.txt", "r", stdin); int n, m;
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++) scanf("%s", G[i] + ); if(G[][] != G[n][m]) { puts(""); return ; } d[][][n] = ;
int cur = ;
for(int s = ; s <= (n + m) / ; s++)
{
cur ^= ;
memset(d[cur], , sizeof(d[cur]));
for(int x1 = ; x1 <= s; x1++)
for(int x2 = n; x2 >= n - s + ; x2--)
{
int y1 = s + - x1;
int y2 = n + m + - s - x2;
if(x1 > x2 || y1 > y2) continue;
if(G[x1][y1] == G[x2][y2])
{
add(d[cur][x1][x2], d[cur^][x1][x2]);
add(d[cur][x1][x2], d[cur^][x1-][x2]);
add(d[cur][x1][x2], d[cur^][x1][x2+]);
add(d[cur][x1][x2], d[cur^][x1-][x2+]);
}
}
} int ans = ;
if((n + m) & )
{
for(int i = ; i <= n; i++) add(ans, d[cur][i][i]), add(ans, d[cur][i][i+]);
}
else
{
for(int i = ; i <= n; i++) add(ans, d[cur][i][i]);
} printf("%d\n", ans); return ;
}

代码君

CodeForces 570E DP Pig and Palindromes的更多相关文章

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

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

  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. CF 316div2 E.Pig and Palindromes

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

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

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

  6. codeforces 569C C. Primes or Palindromes?(素数筛+dp)

    题目链接: C. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes in ...

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

  8. Two Melodies CodeForces - 813D (DP,技巧)

    https://codeforces.com/problemset/problem/813/D dp[i][j] = 一条链以i结尾, 另一条链以j结尾的最大值 关键要保证转移时两条链不能相交 #in ...

  9. Consecutive Subsequence CodeForces - 977F(dp)

    Consecutive Subsequence CodeForces - 977F 题目大意:输出一序列中的最大的连续数列的长度和与其对应的下标(连续是指 7 8 9这样的数列) 解题思路: 状态:把 ...

随机推荐

  1. ef 操作 mysql 中文乱码问题

    1.保证mysql数据的编码为utf8 启动mysql mysql -hlocalhost -uroot -p 输入密码 show VARIABLES like 'character_%'; SET  ...

  2. MVC FileResult

    你如何将文件传送给用户取决于你最开始如何存储它,如果你将文件存入数据库,你会用流的方式将文件返还给用户,如果你将文件存在硬盘中,你只需要提供一个超链接即可,或者也可以以流的方式.每当你需要以流的方式将 ...

  3. (wp8.1)样式资源

    在开发wp的时候,难免要设置好多属性.但是有好多属性是重复的,我们可不可统一 设置样式呢,答案是肯定的 代码如下 <Page x:Class="Blue.MainPage" ...

  4. 移动端的300ms延迟和点击穿透

    移动端300ms延迟:假定这么一个场景.用户在 浏览器里边点击了一个链接.由于用户可以进行双击缩放或者双击滚动的操作,当用户一次点击屏幕之后,浏览器并不能立刻判断用户是确实要打开这个链接,还是想要进行 ...

  5. JavaScript中函数对象和对象的区别

    function Test (word) { console.log (word); } Test('哈哈,我是函数'); new Test('哈哈,我是对象'); //将以上的调用方式换种通俗易懂的 ...

  6. java控制远程ssh-JSCH(二)

    github: https://github.com/wengyingjian/ssh-java-demo.git 这次找到了一套新的api,叫jsch.网上查了一下,顺便把官网的几个demo给一通拿 ...

  7. 编写sql查询语句思路

    编写查询语句思路/* 1.首先确定最终输出结果的列,包括几个方面:A.首先这些列来自于一个 表.还是多个表,如果是多个表则可能用到多表查询的(等值连接.不等值 连接.外连接.自连接):B.这些列是直接 ...

  8. 2017.10.1 QBXT 模拟赛

    题目链接 T1 枚举右端点,前缀和优化.对于当前点x,答案为 sum[x][r]-sum[x][l-1]-(sum[z][r]-sum[z][l-1]) 整理为 sum[x][r]-sum[z][r] ...

  9. 安装指定版本的minikube

    Minikube是什么? Kubernetes集群的安装和部署对于很多初学者来说是一道坎.为了方便大家开发和体验Kubernetes,Kubernetes开源社区提供了可以在本地部署的Minikube ...

  10. Android中一个关于ListView的奇怪问题

    今天在做项目的时候发现了一个比较奇怪的问题,是关于ListView的,即ListView的android:height属性会影响程序中ListView的getView()方法的调用次数,如果设置Lis ...