CF570E Pig and Palindromes
完全不会这种类型的$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的更多相关文章
- 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 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 ...
- 【25.64%】【codeforces 570E】Pig and Palindromes
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces 570E - Pig and Palindromes - [滚动优化DP]
题目链接:https://codeforces.com/problemset/problem/570/E 题意: 给出 $n \times m$ 的网格,每一格上有一个小写字母,现在从 $(1,1)$ ...
- 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 ...
- CodeForces 570E DP Pig and Palindromes
题意:给出一个n行m列的字符矩阵,从左上角走到右下角,每次只能往右或者往下走,求一共有多少种走法能得到回文串. 分析: 可以从两头开始考虑,每次只走一样字符的格子,这样得到的两个字符串拼起来之后就是一 ...
- Codeforces 570 - A/B/C/D/E - (Done)
链接:https://codeforces.com/contest/570 A - Elections - [水] AC代码: #include<bits/stdc++.h> using ...
- Hadoop学习笔记—16.Pig框架学习
一.关于Pig:别以为猪不能干活 1.1 Pig的简介 Pig是一个基于Hadoop的大规模数据分析平台,它提供的SQL-LIKE语言叫Pig Latin,该语言的编译器会把类SQL的数据分析请求转换 ...
随机推荐
- memcached使用libevent 和 多线程模式
一.libevent的使用 首先我们知道,memcached是使用了iblievet作为网络框架的,而iblievet又是单线程模型的基于linux下epoll事件的异步模型.因此,其基本的思想就是 ...
- 数据清洗记录,pandas
pandas数据清洗:http://www.it165.net/pro/html/201405/14269.html data=pd.Series([1,2,3,4]) data.replace([1 ...
- LeetCode K-diff Pairs in an Array
原题链接在这里:https://leetcode.com/problems/k-diff-pairs-in-an-array/#/description 题目: Given an array of i ...
- Bootstrap确定样式让屏幕缩小后布局不乱
解决方案是如下 结果如下:
- Tomcat 工作原理 1 (转)
Tomcat 系统架构与设计模式,第 1 部分: 工作原理 这个分为两个部分的系列文章将研究 Apache Tomcat 的系统架构以及其运用的很多经典设计模式.本文是第 1 部分,将主要从 Tomc ...
- Message类的属性Msg所关联的消息ID
在做C#的Message消息处理的时候,用到了消息的msg编号不知道对应的是什么事件,所以才从网上找来资料如下,在文章最后我会给出资料的出处的. WM_NULL=0x0000 WM_CREATE=0x ...
- CF gym 101933 K King's Colors —— 二项式反演
题目:http://codeforces.com/gym/101933/problem/K 其实每个点的颜色只要和父亲不一样即可: 所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \) ...
- git公钥生成以及与coding等联合
更好用的 https://segmentfault.com/a/1190000002645623 其中 $ ssh -T git@github.comgitbub $ ssh -T git@git.c ...
- maven工程编译成jar包
在pom文件的project节点下增加build节点,mvn package即可 <build> <plugins> <plugin> <artifactId ...
- Poj 2395 Out of Hay( 最小生成树 )
题意:求最小生成树中最大的一条边. 分析:求最小生成树,可用Prim和Kruskal算法.一般稀疏图用Kruskal比较适合,稠密图用Prim.由于Kruskal的思想是把非连通的N个顶点用最小的代价 ...