CodeForces 570E DP Pig and Palindromes
题意:给出一个n行m列的字符矩阵,从左上角走到右下角,每次只能往右或者往下走,求一共有多少种走法能得到回文串。
分析:
可以从两头开始考虑,每次只走一样字符的格子,这样得到的两个字符串拼起来之后就是一个回文串。
设d(step, x1, y2, x2, y2)表示从左上角(1, 1)z往右下走step个格子走到(x1, y1),同时从右下角(n, m)往左上走step个格子走到(x2, y2),而且两边得到一样字符串的方法数。
一开始判断一下左上角和右下角的字符是不是一样,若一样,d(1, 1, 1, n, m) = 1,若不一样则直接输出0.
状态转移:
如果s[x1][y2] = s[x2][y2],而且x1 ≤ y1 && y1 ≤ y2
d(step, x1, y1, x2, y2) = d(step - 1, x1 - 1, y1, x2 + 1, y2) + d(step - 1, x1, y1 - 1, x2 + 1, y2) + d(step - 1, x1 - 1, y1, x2, y2 + 1) + d(step - 1, x1, y1- 1, x2, y2+ 1)
这是一个五维的状态空间太大了,空间优化:
首先如果已知step的话,x1和y1只要知道其中一个,就可以计算出另一个,x2和y2同理,所以我们可以把y1和y2去掉,这样把五维优化成三维。
另外一个优化就是,我们在递推的时候是按照step从小到大递推的,所以可以用滚动数组来优化,这样空间复杂度为O(n^2)。
另外要考虑一下奇偶的问题:
如果整个字符串长度为奇数的话,两个字符串会相遇在同一个格子
如果整个字符串长度为偶数的话,两个字符串会走到相邻的格子,要么是同一行左右相邻,要么是同一列上下相邻
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; typedef long long LL; const int maxn = + ;
const int MOD = ; int d[][maxn][maxn]; char G[maxn][maxn]; void add(int& a, int b) { a += b; if(a >= MOD) a -= MOD; } int main()
{
//freopen("in.txt", "r", stdin); int n, m;
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++) scanf("%s", G[i] + ); if(G[][] != G[n][m]) { puts(""); return ; } d[][][n] = ;
int cur = ;
for(int s = ; s <= (n + m) / ; s++)
{
cur ^= ;
memset(d[cur], , sizeof(d[cur]));
for(int x1 = ; x1 <= s; x1++)
for(int x2 = n; x2 >= n - s + ; x2--)
{
int y1 = s + - x1;
int y2 = n + m + - s - x2;
if(x1 > x2 || y1 > y2) continue;
if(G[x1][y1] == G[x2][y2])
{
add(d[cur][x1][x2], d[cur^][x1][x2]);
add(d[cur][x1][x2], d[cur^][x1-][x2]);
add(d[cur][x1][x2], d[cur^][x1][x2+]);
add(d[cur][x1][x2], d[cur^][x1-][x2+]);
}
}
} int ans = ;
if((n + m) & )
{
for(int i = ; i <= n; i++) add(ans, d[cur][i][i]), add(ans, d[cur][i][i+]);
}
else
{
for(int i = ; i <= n; i++) add(ans, d[cur][i][i]);
} printf("%d\n", ans); return ;
}
代码君
CodeForces 570E DP Pig and Palindromes的更多相关文章
- 【25.64%】【codeforces 570E】Pig and Palindromes
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 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 ...
- 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 570E - Pig and Palindromes - [滚动优化DP]
题目链接:https://codeforces.com/problemset/problem/570/E 题意: 给出 $n \times m$ 的网格,每一格上有一个小写字母,现在从 $(1,1)$ ...
- codeforces 569C C. Primes or Palindromes?(素数筛+dp)
题目链接: C. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes in ...
- 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 ...
- Two Melodies CodeForces - 813D (DP,技巧)
https://codeforces.com/problemset/problem/813/D dp[i][j] = 一条链以i结尾, 另一条链以j结尾的最大值 关键要保证转移时两条链不能相交 #in ...
- Consecutive Subsequence CodeForces - 977F(dp)
Consecutive Subsequence CodeForces - 977F 题目大意:输出一序列中的最大的连续数列的长度和与其对应的下标(连续是指 7 8 9这样的数列) 解题思路: 状态:把 ...
随机推荐
- 如何优化Mysql执行查询数据的速度
在项目中数据量小的情况下使用like查询速度还行,但是随着数据一天一天增加,再使用like进行模糊查询的时候速度上就会显得比较慢,现提供两套解决方案: 问题: 使用like查询效率很慢 select ...
- NHibernate中创建User类报错问题
前两天刚开始学习NHibernate架构,照着前辈的例子打了一遍运行之后没问题,然后自己创建了一个User的Model发现一运行就报User附近有错误,然后就检查,类写的没错用了virtual,Use ...
- Java基础(变量、运算符)
第2天 Java基础语法 今日内容介绍 u 变量 u 运算符 第1章 变量 1.1 变量概述 前面我们已经学习了常量,接下来我们要学习变量.在Java中变量的应用比常量的应用要多很多.所以变量也是尤为 ...
- 3、HTTP content-type
HTTP content-type Content-Type,内容类型,一般是指网页中存在的Content-Type,用于定义网络文件的类型和网页的编码,决定浏览器将以什么形式.什么编码读取这个文件, ...
- Java连接数据库,增删改查
底层代码: package com.zdsoft; import java.sql.*; /** * Created by lx on 2017/6/22. */ public class JDBCU ...
- Servlet--HttpServlet
一.Servlet 接口(javax.servlet) 定义: public interface Servlet Implemented by: FacesServlet, Gene ...
- ArcGIS for Server 10.3.X 新型紧凑型缓存的解读和应用
早在2010年年底,牛魔王中王在其博客空间牛魔王的作坊中对ArcGIS 10中推出的紧凑型缓存格式进行了详细的解读,详见<ArcGIS 切片缓存紧凑文件格式分析与使用>.紧随着的4年时间里 ...
- Python+selenium之获取验证信息
通常获取验证信息用得最多的几种验证信息分别是title,URL和text.text方法用于获取标签对之间的文本信息. 代码如下: from selenium import webdriverimpor ...
- 地址栏传值 JS取值方法
function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&] ...
- 基于FPGA的DDS任意波形发生器设计
一.简介 DDS技术最初是作为频率合成技术提出的,由于其易于控制,相位连续,输出频率稳定度高,分辨率高, 频率转换速度快等优点,现在被广泛应用于任意波形发生器(AWG).基于DDS技术的任 ...