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的数据分析请求转换 ...
随机推荐
- RedHat 6.8 内核编译
/*************************************************************************** * RedHat 6.8 内核编译 * 说明: ...
- ThinkPHP中的find和select的区别
ThinkPHP作为PHP中应用广泛又好用的框架,能比较快速的开发MVC架构的管理系统,获得了大量的应用.但是在ThinkPHP中select()和find()方法有什么区别呢? 事实上find()返 ...
- Unity物体上下反复漂浮效果
using UnityEngine;using System.Collections;// 主界面的开始按钮使用该脚本,控制上下来回浮动public class Floating : MonoBeha ...
- python if语句,while语句
一,if语句: python中最常用的判断语句,基本格式: 1.if else格式 if 条件: 结果 else: 结果 """ if 条件: 满足条件执行代码 els ...
- CF 622F The Sum of the k-th Powers——拉格朗日插值
题目:http://codeforces.com/problemset/problem/622/F 发现 sigma(i=1~n) i 是一个二次的多项式( (1+n)*n/2 ),sigma(i=1 ...
- java代码继承疑惑,请有心人解答
总结:这段程序没有问题,编译运行都是可以的.关键是,子类的无参构造方法第一句少了super(a,b);运行后,显示了双重结果 .还有.如果子类中没有声明成员变量String name.那么结果显示父 ...
- c语言-树的基础知识
第一.树的定义: 1.有且只有一个称为根的节点 2.有若干个互不相交的子树,这些子树本身也是一颗树 第二.专业术语: 树的深度:从根节点到最低层,节点的层数 ,称之为树的深度. 根节点是第一 ...
- appium_python_android测试环境搭建
第一步 安装appium •Appium是由.NET 开发的,所以,它会依赖 .NET framework相关组件,所以先安装.net framework 4.5,备注: Appium最低支持.ne ...
- DAY19-Django之form组件补充
问题1:注册页面输入为空,报错:keyError:找不到password def clean(self): print("---" ,self.cleaned_data) # if ...
- LinkedHashMap和HashMap的区别
一.问题描述: 前几天写webservices接口,需要同步人力资源,涉及到添加顺序:主账号需要添加在次账号之前,直接上级需要添加在下级之前.解析xml之后直接封装在HashMap中,导致取对象时顺序 ...