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 ...
随机推荐
- html 的table tr td的使用
cellspacing 属性规定单元格之间的空间. cellpadding 属性规定单元边沿与其内容之间的空白. border 简写属性在一个声明设置所有的边框属性. border 用在table属性 ...
- index and polymorphic
http://guides.rubyonrails.org/association_basics.html#polymorphic-associations class CreateStars < ...
- POJ 1456 Supermarket 区间问题并查集||贪心
F - Supermarket Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- HDOJ 2095
find your present (2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/1024 K (Java/Oth ...
- sed替换字符串时,使用正则表达式的注意事项
sed的使用方法为: 使用单个模式替换:sed 's/pattern/replacement/flags' filename,例如echo 'abc' | sed 's/a/A/'-->Abc ...
- github student pack中的digital ocean可以使用银联卡支付
申请了 github student pack却因为一直没有visita信用卡,而无法使用digital ocean的 $50,一直到今天,用中国银行借记卡成功支付. 方法是: (1)注册paypal ...
- 《ASP.NET1200例》ListView 控件与DataPager控件的结合<一>
分页 在前一部分开始时介绍的原 HTML 设计中内含分页和排序,所以根据规范完整实现该网格的任务尚未完成.我们先分页,然后再排序. ListView 控件中的分页通过引入另一个新控件 Data ...
- 跨域请求获取Solr json检索结果并高亮显示
Solr提供了json格式的检索结果,然而在跨域的情况下如何调用呢?我们可以利用jquery提供的jsonp的方式获取Solr检索结果. <script type="text/java ...
- iOS中的NSTimer 和 Android 中的Timer
首先看iOS的, Scheduling Timers in Run Loops A timer object can be registered in only one run loop at a t ...
- Greedy:Fence Repair(POJ 3252)
Fence Repair 问题大意:农夫约翰为了修理栅栏,要将一块很长的木块切割成N块,准备切成的木板的长度为L1,L2...LN,未切割前的木板的长度恰好为切割后木板的长度的总和,每次切断木板的时候 ...