题意:给出一个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. C#数据类型 值传递和引用传递

    /// <summary> /// 电脑类 /// </summary> public class Computer { public string Type { get; s ...

  2. 第十五章 提升用户体验 之 设计实现MVC controllers 和 actions

    1. 概述 controllers 和 actions 是 ASP.NET MVC4中非常重要的组成部分. controller管理用户和程序间的交互,使用action作为完成任务的方式. 如果是包含 ...

  3. jquery阻止网页中右键的点击

    <body onmousedown="whichElement(event)"> </body> function whichElement(e) { if ...

  4. webpack.config.js====entry入口文件的配置

    1.  一般是采用对象语法: entry: { index: './src/default/js/index.js' }, https://webpack.css88.com/concepts/ent ...

  5. JAVA 框架之面向对象设计原则

     面向对象设计原则:  单一职责原则 SRP :   一个类或者行为只做一件事 .  降低代码冗余,提高可重用性,可维护性,可扩展性,可读性 使用组合形式   里氏替换原则 LSP :  所有引用基类 ...

  6. C++程序中调用WebService的实现

    前言 因为最近的项目中需要运用到在MFC程序中调用WebService里面集成好了的函数,所以特意花了一天的时间来研究WebService的构建以及如何在MFC的程序中添加Web引用,进而来实现在C+ ...

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

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

  8. Ubuntu 设置静态ip地址

    1. 找到文件并作如下修改: sudo vim /etc/network/interfaces 修改如下部分: auto eth0iface eth0 inet staticaddress 192.1 ...

  9. Got error 28 from storage engine的错误处理

    早上例行检查数据库,发现Got error 28 from storage engine这个错误,天那,我的数据.心里哇凉....备份的时间还是很久以前.最近更新了不少,麻烦大了. 好在找到了解决方法 ...

  10. iBatis for net 框架使用

    简介:ibatis 一词来源于“internet”和“abatis”的组合,是一个由Clinton Begin在2001年发起的开放源代码项目,到后面发展的版本叫MyBatis但都是指的同一个东西.最 ...