LightOJ1044 Palindrome Partitioning(区间DP+线性DP)
问题问的是最少可以把一个字符串分成几段,使每段都是回文串。
一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时候要枚举,这样时间复杂度是不可行的。
然后我就想降维度了,只能线性DP,dp[i]表示子串[0,i]的答案。这样可以从i-1转移到i,str[i]单独作一段或者str[i]能和前面的组成回文串,方程如下:
dp[i]=min(dp[i-1]+1,dp[j-1]+1) (子串[j,i]是回文串)
现在问题是怎么快速判断一个字符串的任意子串是否是回文串。
我想该不会要用字符串的一些数据结构或算法吧。。忽然又想到区间DP,这个问题是可以用区间DP解决的:
dp2[i][j]表示子串[i,j]是否是回文串
而转移只要一步即可:
dp2[i][j] = (str[i]==str[j] && dp2[i+1][j-1])
因此这就可以在O(strlen2)预处理完并在O(1)时间复杂度下判断任意区间是否是回文串。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char str[];
bool palindrome[][];
int d[];
int main(){
int t;
scanf("%d",&t);
for(int cse=; cse<=t; ++cse){
scanf("%s",str);
int n=strlen(str); for(int i=; i<n; ++i){
for(int j=; j<n; ++j){
palindrome[i][j]=(i>=j);
}
}
for(int len=; len<=n; ++len){
for(int i=; i+len<=n; ++i){
if(str[i]==str[i+len-] && palindrome[i+][i+len-]) palindrome[i][i+len-]=;
}
} d[]=;
for(int i=; i<n; ++i){
if(palindrome[][i]){
d[i]=;
continue;
}
d[i]=d[i-]+;
for(int j=i-; j>=; --j){
if(palindrome[j][i]) d[i]=min(d[i],d[j-]+);
}
}
printf("Case %d: %d\n",cse,d[n-]);
}
return ;
}
LightOJ1044 Palindrome Partitioning(区间DP+线性DP)的更多相关文章
- uva 11584 Partitioning by Palindromes 线性dp
// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...
- Atcoder Yet Another Palindrome Partitioning(状压dp)
Atcoder Yet Another Palindrome Partitioning 思路: 一个字符串满足条件的情况是奇数字母个数小于等于1,也就是异或起来是1<<j(0<=j& ...
- hdu1712 线性dp
//Accepted 400 KB 109 ms //dp线性 //dp[i][j]=max(dp[i-1][k]+a[i][j-k]) //在前i门课上花j天得到的最大分数,等于max(在前i-1门 ...
- [CodeForces - 1272D] Remove One Element 【线性dp】
[CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...
- 1. 线性DP 300. 最长上升子序列 (LIS)
最经典单串: 300. 最长上升子序列 (LIS) https://leetcode-cn.com/problems/longest-increasing-subsequence/submission ...
- 1044 - Palindrome Partitioning(区间DP)
题目大意: 给你一个字符串,问这个字符串最少有多少个回文串. 区间DP直接搞 #include<cstdio> #include<cstring> #include&l ...
- Lightoj 1044 - Palindrome Partitioning (DP)
题目链接: Lightoj 1044 - Palindrome Partitioning 题目描述: 给一个字符串,问至少分割多少次?分割出来的子串都是回文串. 解题思路: 先把给定串的所有子串是不 ...
- 132. Palindrome Partitioning II (String; DP)
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 131. Palindrome Partitioning (Back-Track, DP)
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
随机推荐
- SGU 179 Brackets light(生成字典序的下一个序列)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=179 解题报告:输入一个合法的括号串,求出这个括号串的字典序的下一个串.(认为'(' ...
- 查看远程git log
$ git reset --hard bit/add $ git log --oneline --graph
- 关于seajs
(这些文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) 最近经常听到各种JS前缀的名称,瞬间感觉自己弱爆了,啥都没用过呢,这么下去将来怎么嫁人呢. ...
- mac安装软件管家homebrew
http://www.iwangzheng.com/ 1.简介 众所周知,Mac的操作系统是基于Unix的,在这个系统上大家可以安装一些专门为mac定制开发的软件.这就带来了一个问题,手工编译每个软件 ...
- 如何选择Html.RenderPartial和Html.RenderAction
Html.RenderPartial与Html.RenderAction这两个方法都是用来在界面上嵌入用户控件的. Html.RenderPartial是直接将用户控件嵌入到界面上: <%Htm ...
- object-c 系列教程
1.object-c 基本数据类型 2.object-c 控制语句 3.object-c面向对象1 4.object-c面向对象2 5.object-c 继承多态 动态数据类型
- c++关键字之#define typedef const
[#define] #define是预处理指令,在编译预处理时进行简单的替换,不作正确性检查. [typedef] typedef只是为了增加可读性而为标识符另起的新名称 在自己的作用域内给一个已经存 ...
- Ubuntu 14.04的vim编辑器配置Python开发环境
#1 $ sudo apt-get install exuberant-ctags vim-scripts $ vim-addons install taglist #2 到:http://www.v ...
- 制做RPM包
http://nmshuishui.blog.51cto.com/1850554/1583117 [root@NGM ~]# yum install pcre-devel zlib-devel ope ...
- ubuntu下简单的驱动编译
转自:http://www.eefocus.com/jefby1990/blog/13-02/291628_c39b8.html 本文是参考了网上多篇帖子而写的算不上什么原创.唯一值得欣慰的只不过在本 ...