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的数据分析请求转换 ...
随机推荐
- Linux C 编程内存泄露检测工具(一):mtrace
前言 所有使用动态内存分配(dynamic memory allocation)的程序都有机会遇上内存泄露(memory leakage)问题,在Linux里有三种常用工具来检测内存泄露的情況,包括: ...
- 【转】Python获取当前系统时间
转自:https://www.cnblogs.com/-ldzwzj-1991/p/5889629.html 取得时间相关的信息的话,要用到python time模块,python time模块里面有 ...
- Redis底层探秘(五):Redis对象
前面几篇文章,我们一起学习了redis用到的所有主要数据结构,比如简单动态字符串(sds).双端链表.字典.压缩列表.整数集合等等. redis并没有直接使用这些数据结构来实现键值对数据库,而是基于这 ...
- Mxgraph使用总结一
一.Mxgraph介绍: mxGraph 是一个 JS 绘图组件适用于需要在网页中设计/编辑 Workflow/BPM流程图.图表.网络图和普通图形的 Web 应用程序.mxgraph 下载包中包括j ...
- 【转】Tomcat和Weblogic的区别
J2ee开发主要是浏览器和服务器进行交互的一种结构.逻辑都是在后台进行处理,然后再把结果传输回给浏览器.可以看出服务器在这种架构是非常重要的. 这几天接触到两种Java的web服务器,做项目用的Tom ...
- JSF拦截ajax请求并传递参数方法
我们可以利用f:ajax做一些简单的ajax操作,但是遇到复杂的逻辑,它不能简单的去实现,jsf提供了一种方法,可以调用它内部的js方法去实现复杂的逻辑. 首先要在页面引入jsf的js文件: < ...
- Day2-VIM(五):复制
粘帖 p 粘帖 复制的形式可以很多,但是粘帖的形式却没多少 无非就是加数字达到多次粘帖什么的 其实准确的说,p应该是放置的意思 不过我也搞不清是paste还是put的缩写 单词和字符复制 ynl 向右 ...
- Java-API-Package:org.springframework.stereotype
ylbtech-Java-API-Package:org.springframework.stereotype 1.返回顶部 1. @NonNullApi @NonNullFields Package ...
- Redis codis 搭建测试
codis Codis 是一个分布式 Redis 解决方案, 对于上层的应用来说, 连接到 Codis Proxy 和连接原生的 Redis Server 没有明显的区别,有部分命令支持 Codis ...
- ThreadPoolExecutor的corePoolSize和maximumPoolSize
按照JDK文档的描述, 如果池中的实际线程数小于corePoolSize,无论是否其中有空闲的线程,都会给新的任务产生新的线程 如果池中的线程数>corePoolSize and <max ...