题意:给出一个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. Redis特性之持久化机制

    持久化机制 Redis是一个支持持久化的内存数据库,也就是说redis需要经常将内存中的数据同步到硬盘来保证持久化. Redis支持两种持久化方式: 1.snapshotting(快照)也是默认方式 ...

  2. IQueryable和IEnumerable的区别

  3. 数据库操作是sql的操作1

    项目过来以后, 查 先肯定是做UI , 1.在UI层先加载 2数据来源在dal层, 3再到model层,得到属性 public int MId { get; set; } public string ...

  4. 使用Karabiner为Mac内置键盘、HHKB进行映射

    使用Karabiner为Mac内置键盘.HHKB进行映射 Table of Contents 1. 引言 2. 什么是Karabiner和配置方法的基本说明 3. 内置键盘设置 4. HHKB设置 5 ...

  5. Bootstrap下拉菜单相关

    1.实现普通下拉菜单:.dropdown>button.dropdown-toggle[data-toggle="dropdown"]+ul.dropdown-menu; 2 ...

  6. Azure 虚拟机安全加固整理

    这篇文档不是原创,只是基于Azure官网上的Doc进行了相关链接的整理,从简单层面的安全设置,到更高层面的安全架构考量,以及Azure安全的白皮书及最佳实践,送给需要的你们,定有一款适合你! 做好数据 ...

  7. ubuntu下irobot串口通讯

    在window下以前就`有一个现成的串口代码.想移植到ubuntu下,发现都不一样了.要重新找个. 折腾了一上午之后,发现自己写这个串口通讯还是有一点难度. 于是,用了github上 Erick Co ...

  8. C语言中字符串数组的遍历和比较

    /* The list of known types of default packet. */static char  *_default_packet_types[] = {    "d ...

  9. SAP OData编程指南

    OData(Open Data Protocol)协议是一个开放的工业标准,用于定义RESTFul API的设计和使用.我的文章标题前加上SAP的前缀,只是为了表明这篇文章介绍的是Jerry在SAP项 ...

  10. xwork-conversion.properties 目前没有解决方案

    它没法变成.xml 这意味着项目里就只能这样