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 ...
随机推荐
- sql 批量更新
利用update和select进行批量更新 UPDATE Table1 SET categoryId =(SELECT Id FROM Table2 WHERE Table1 .Id=Table2.c ...
- xxxx is not translated in zh-rCN, zh-rTW
1.异常提示: "image_content" is not translated in zh-rCN, zh-rTW 2.错误原因: 根据报错提示,是说我没有对string文件做 ...
- poj2253 最短路 floyd Frogger
Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28825 Accepted: 9359 Descript ...
- 【OpenStack】OpenStack系列3之Swift详解
Swift安装部署(与keystone依赖包有冲突,需要安装不同版本eventlet) 参考:http://www.server110.com/openstack/201402/6662.html h ...
- 【Other】推荐点好听的钢琴曲
2013-12-13 16:19 匿名 | 浏览 138977 次 音乐钢琴 推荐点好听的钢琴曲,纯音乐也可以thanks!!! 2013-12-14 19:34 网友采纳 热心网友 巴洛克:帕海贝尔 ...
- webdriver+python 对三大浏览器的支持
1.在IE浏览器上运行测试脚本,首先需要下载IEDriverServer.exe(http://code.google.com/p/selenium/downloads/list,根据浏览器的版本下载 ...
- mysql5.6 timestamp
1.timestamp 默认值 CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 在创建新记录和修改现有记录的时候都对这个数据列刷新 CURRENT_TIME ...
- Price List
Price List Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)Tota ...
- JavaScript当离开页面时可以进行的操作
当JavaScript离开页面时可以进行的操作 window.onbeforeunload = function() { var email = document.getElementById(&qu ...
- OSG addEventHandler W键 L键 F键
// add the state manipulator viewer->addEventHandler( new osgGA::StateSetManipulator(viewer-&g ...