CodeForces 570E DP Pig and Palindromes
题意:给出一个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的更多相关文章
- 【25.64%】【codeforces 570E】Pig and Palindromes
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces 570 E. Pig and Palindromes (DP)
题目链接: 570 E. Pig and Palindromes 题目描述: 有一个n*m的矩阵,每个小格子里面都有一个字母.Peppa the Pig想要从(1,1)到(n, m).因为Peppa ...
- 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 ...
- CF 316div2 E.Pig and Palindromes
E. Pig and Palindromes Peppa the Pig was walking and walked into the forest. What a strange coincide ...
- Codeforces 570E - Pig and Palindromes - [滚动优化DP]
题目链接:https://codeforces.com/problemset/problem/570/E 题意: 给出 $n \times m$ 的网格,每一格上有一个小写字母,现在从 $(1,1)$ ...
- codeforces 569C C. Primes or Palindromes?(素数筛+dp)
题目链接: C. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes in ...
- 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 ...
- Two Melodies CodeForces - 813D (DP,技巧)
https://codeforces.com/problemset/problem/813/D dp[i][j] = 一条链以i结尾, 另一条链以j结尾的最大值 关键要保证转移时两条链不能相交 #in ...
- Consecutive Subsequence CodeForces - 977F(dp)
Consecutive Subsequence CodeForces - 977F 题目大意:输出一序列中的最大的连续数列的长度和与其对应的下标(连续是指 7 8 9这样的数列) 解题思路: 状态:把 ...
随机推荐
- 洪水 Pow
Description AKD市处在一个四面环山的谷地里.最近一场大暴雨引发了洪水,AKD市全被水淹没了.Blue Mary,AKD市的市长,召集了他的所有顾问(包括你)参加一个紧急会议.经过细致的商 ...
- 【0 基础学Dojo】第【1】篇 HelloWord
打开dojo 官网首页 http://dojotoolkit.org/,我们看到 点击get dojo 你将得到下载Dojo 的不同方式 2,点击下面方式下载, 解压后 新建myTest.html, ...
- H5gulp版非前后的分离环境
由于公司不同意我们使用前后端分离进行开发,硬是要我们和PHP混合在一起,所以用gulp搭建了一个简单的手脚架来用 目录结构: 主要是gulpfile.js里的内容 var gulp = require ...
- scrollHelper
(function ($) { var mouseScroll = function (e) { try { var origEvent = e.originalEvent; origEvent.pr ...
- Android使用AchartEngine绘制曲线图
1.在布局文件中加入LinearLayout布局,如下: <LinearLayout android:id="@+id/chart" android:orientation= ...
- 如何解决Eureka Server不踢出已关停的节点的问题?
如何解决Eureka Server不踢出已关停的节点的问题? eureka端: eureka.server.enable-self-preservation ...
- selenium +python之Page Obiect设计模式
PageObject是selenium自动化测试项目开发实践的最佳设计模式之一,它主要体现对界面交互细节的封装,这样可以使测试案例更关注于业务而非界面细节,从而提高测试案例的可读性. 1.认识Page ...
- SPOJ SORTBIT Sorted bit squence (数位DP,入门)
题意: 给出一个范围[m,n],按照二进制表示中的1的个数从小到大排序,若1的个数相同,则按照十进制大小排序.求排序后的第k个数.注意:m*n>=0. 思路: 也是看论文的.一开始也能想到是这种 ...
- 2017.10.3 QBXT 模拟赛
题目链接 T1 模拟 #include <cstring> #include <cstdio> #define N 105000 int L,R; char s[N]; int ...
- C#注册表操作类(完整版)
下面贴出自己用C#写的注册表操作类,欢迎大家拍砖! 1.注册表基项静态域 1 /// <summary> 2 /// 注册表基项静态域 3 /// 4 /// 主要包括: 5 /// 1. ...