Codeforces 570E - Pig and Palindromes - [滚动优化DP]
题目链接:https://codeforces.com/problemset/problem/570/E
题意:
给出 $n \times m$ 的网格,每一格上有一个小写字母,现在从 $(1,1)$ 位置走到 $(n,m)$ 位置,要求经过路径构成一个回文串。
要求走路方向保证坐标不会减小(即只能往下或者往右走),要求你给出路径方案数目。
题解:
考虑 $f[x_1][y_1][x_2][y_2]$ 表示一端从 $(1,1)$ 出发,走到了 $(x_1,y_1)$,同时另一端从 $(n,m)$ 出发,走到了 $(x_2,y_2)$,此时形成回文串的数目。
这个是不难维护的,主要问题是这个维数过多,开不下。然后不难发现,如果用 $f[step][x_1][x_2]$ 来维护,可以通过步数 $step$ 和 $x$ 算出 $y$,
然后更进一步可以发现 $step$ 不需要全部维护,可以使用滚动优化。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+;
const int SIZE=; int n,m;
char mp[SIZE][SIZE];
ll f[][SIZE][SIZE]; inline bool In(int x,int y)
{
return <=x && x<=n && <=y && y<=m;
}
int main()
{
cin>>n>>m;
for(int i=;i<=n;i++) scanf("%s",mp[i]+); int now=;
memset(f[now],,sizeof(f[now]));
f[now][][n]=(ll)(mp[][]==mp[n][m]);
for(int step=;step<=(n+m)/-;step++)
{
now^=;
memset(f[now],,sizeof(f[now]));
for(int x1=;x1<=n;x1++)
{
for(int x2=n;x2>=;x2--)
{
int y1=step+-x1, y2=n+m-step-x2;
if(!In(x1,y1) || !In(x2,y2)) continue;
if(mp[x1][y1]!=mp[x2][y2]) continue; f[now][x1][x2]+=f[now^][x1][x2];
f[now][x1][x2]%=mod; f[now][x1][x2]+=f[now^][x1-][x2+];
f[now][x1][x2]%=mod; f[now][x1][x2]+=f[now^][x1-][x2];
f[now][x1][x2]%=mod; f[now][x1][x2]+=f[now^][x1][x2+];
f[now][x1][x2]%=mod;
}
}
} ll ans=;
if((n+m)%)
{
for(int i=;i<=n;i++) ans+=f[now][i][i], ans%=mod;
for(int i=;i<n;i++) ans+=f[now][i][i+], ans%=mod;
}
else
{
for(int i=;i<=n;i++) ans+=f[now][i][i], ans%=mod;
}
cout<<ans<<endl;
}
Codeforces 570E - Pig and Palindromes - [滚动优化DP]的更多相关文章
- CodeForces 311 B Cats Transport 斜率优化DP
题目传送门 题意:现在有n座山峰,现在 i-1 与 i 座山峰有 di长的路,现在有m个宠物, 分别在hi座山峰,第ti秒之后可以被带走,现在有p个人,每个人会从1号山峰走到n号山峰,速度1m/s.现 ...
- Codeforces 643C Levels and Regions 斜率优化dp
Levels and Regions 把dp方程列出来, 把所有东西拆成前缀的形式, 就能看出可以斜率优化啦. #include<bits/stdc++.h> #define LL lon ...
- CodeForces 834D The Bakery(线段树优化DP)
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...
- CodeForces - 459E Pashmak and Graph[贪心优化dp]
E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces 311B Cats Transport【斜率优化DP】
LINK 题目大意 有一些猫,放在一些位置,人一步移动一个位置 给出每个猫出现的时间,每个人可以自由安排其出发时间,沿途已经出现的猫捡起,猫等待的时间是被减去的时间减去出现的时间 猫可以等人,人不能等 ...
- CodeForces - 1175E Minimal Segment Cover (倍增优化dp)
题意:给你n条线段[l,r]以及m组询问,每组询问给出一组[l,r],问至少需要取多少个线段可以覆盖[l,r]区间中所有的点. 如果贪心地做的话,可以求出“从每个左端点l出发选一条线段可以到达的最右端 ...
- Codeforces 351C Jeff and Brackets 矩阵优化DP
题意:你要在纸上画一个长度为n * m的括号序列,第i个位置画左括号的花费是a[i % n], 画右括号的花费是b[i % n],问画完这个括号序列的最小花费.n <= 20, m <= ...
- CodeForces - 512B Fox And Jumping[map优化dp]
B. Fox And Jumping time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- codeforces 570 E. Pig and Palindromes (DP)
题目链接: 570 E. Pig and Palindromes 题目描述: 有一个n*m的矩阵,每个小格子里面都有一个字母.Peppa the Pig想要从(1,1)到(n, m).因为Peppa ...
随机推荐
- Cocoapods pod update执行失败报错CocoaPods was not able to update the `master` repo.2019的解决
很久没动pod,最近更新发现: CocoaPods报CocoaPods was not able to update the `master` repo. If this is an unexpect ...
- go-ehtereum编译:
git clone https://github.com/ethereum/go-ethereum.git cd go-ethereum && git checkout make ge ...
- CentOS 7下升级Python版本到3.x系列
由于python官方已宣布2.x系列即将停止支持,为了向前看,我们升级系统的python版本为3.x系列服务器系统为当前最新的CentOS 7.4 1.安装前查看当前系统下的python版本号 # p ...
- ImageMagick 转换图片格式
[root@ drawable-hdpi-v4]# convert ic_launcher.jpeg ic_launcher.png [root@ drawable-hdpi-v4]# file ic ...
- WWDC 17: 开发者的最初观感
WWDC 17: 开发者的最初观感 前言 每年的 WWDC 都是 iOS 开发者集体高潮的时刻.第一天的 WWDC 带来了全新的 iOS 11.MacOS.tvOS 和 watchOS,革命性的 AR ...
- 使用DNSPod解析Freenom域名
注册Freenom域名 Freenom官网:http://www.freenom.com Freenom提供的顶级域名包括:tk,ml,ga,cf,gq 申请流程: 注册用户后登陆,然后查询并选择一个 ...
- mybatis-plus忽略映射字段
mybatis-plus使用对象属性进行SQL操作,经常会出现对象属性非表字段的情况,忽略映射字段使用以下注解: @TableField(exist = false):表示该属性不为数据库表字段,但又 ...
- ubuntu Ros环境halcon的一个程序
这个首先并不是我一边做实验一遍记录的,而是我做完成以后才想起来做个分享的,所以中途遇到的很多问题,并没有来得及记录下来,现在写的这些都是后话了 首先呢!我们不需要在ROS下写halcon的程序也是可以 ...
- Linux之文件系统各种符号说明
/ 根目录 唯一必须挂载的目录.不要有任何的犹豫,选一个分区,挂载它!(在绝大多数情况下,有10G的容量应该是够用了.当然了,很多东西都是多多益善的) /boot 它包含了操作系统的内核和在启动系统过 ...
- Idea2018旗舰版破解方法
完整请参考 https://www.jianshu.com/p/3c87487e7121 1.在hosts文件里添加一行: 0.0.0.0 account.jetbrains.com 2.在Activ ...