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的数据分析请求转换 ...
随机推荐
- 《精通.NET企业项目开发》 - 书摘精要
(P7) 处于任何逻辑层面上的类,对于同一层面上的其他类应该是可重用的:对于在同等范围内其他所有需要该数据的类而言,提供数据的类应该是可以被调用的: (P9) 大多数企业系统都是用平台无关的技术构建的 ...
- BEC listen and translation exercise 7
Maybe I ought to subscribe to the Engineering Quarterly.也许我应该订阅<工程学季刊>. The performance is sai ...
- codevs 2503 失恋28天-缝补礼物
题目描述 Description 话说上回他给女孩送了n件礼物,由于是廉价的所以全部都坏掉了,女孩很在意这些礼物,所以决定自己缝补,但是人生苦短啊,女孩时间有限,她总共有m分钟能去缝补礼物.由于损坏程 ...
- spring事务-说说Propagation及其实现原理
前言 spring目前已是java开发的一个事实标准,这得益于它的便利.功能齐全.容易上手等特性.在开发过程当中,操作DB是非常常见的操作,而涉及到db,就会涉及到事务.事务在平时的开发过程当中,就算 ...
- IDEA 设置导出
见附件 https://files.cnblogs.com/files/chuancheng/settings.7z
- Java基础--压缩和解压缩gz包
gz是Linux和OSX中常见的压缩文件格式,下面是用java压缩和解压缩gz包的例子 public class GZIPcompress { public static void FileCompr ...
- mybatis association表关联与rowbounds共同使用时的异常及其解决方案
按照mybatis手册中所说的,association有两种实现方式,嵌套查询和嵌套结果映射.如手册中所述,select方式会带来N+1次查询的问题,考虑到效率问题的话建议使用嵌套结果映射.但是在结合 ...
- UE4流关卡
转自:http://blog.ch-wind.com/ue4-level-streaming/ 流关卡可以使得关卡内容只在玩家“需要”的时候才加载,在很多游戏中都有使用这个技术. 当前UE4版本4.1 ...
- java restful response 万能类
import java.util.HashMap; import java.util.Map; public class ResponseData { private final String mes ...
- springmvc----demo3---rest风格---bai
input_stu_path.jsp: showinput_stu_path.jsp: