题意:用最少的不可交线段覆盖整个区间,求该最小值

课上摸鱼的时候没注意到题意的转换,写了没啥卵用的回文中心最长枚举,所以代码里的st和h/h2是几乎没用的

注意状态转移的时候不要只用最长线段去转移,这样未必最优(虽然没找出反例但是用st数组WA了一发)

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int maxn = 1e4+11;
const int oo = 0x3f3f3f3f;
const double eps = 1e-7;
typedef long long ll;
ll read(){
ll x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
char str[maxn];
int h[maxn],h2[maxn],st[maxn],dp[maxn];
bool ok[1111][1111];
int main(){
int T=read();
while(T--){
s1(str);
int n=strlen(str+1);
memset(ok,0,sizeof ok);
rep(i,1,n)st[i]=0;
rep(i,1,n){
h[i]=1;
st[i-h[i]/2]=max(st[i-h[i]/2],h[i]);
ok[i-h[i]/2][i-h[i]/2+h[i]-1]=1;
int left=i-1,right=i+1;
bool flag=0;
while(!flag&&left>0&&right<n+1){
if(str[left]==str[right]){
h[i]=max(h[i],right-left+1);
left--,right++;
st[i-h[i]/2]=max(st[i-h[i]/2],h[i]);
ok[i-h[i]/2][i-h[i]/2+h[i]-1]=1;
}else{
flag=1;
}
} }
rep(i,1,n){//中点为第i条缝
h2[i]=0;
int left=i-1,right=i;
bool flag=0;
while(!flag&&left>0&&right<n+1){
if(str[left]==str[right]){
h2[i]=max(h2[i],right-left+1);
left--,right++;
st[i-h2[i]/2]=max(st[i-h2[i]/2],h2[i]);
ok[i-h2[i]/2][i-h2[i]/2+h2[i]-1]=1;
}else{
flag=1;
} } }
memset(dp,0x3f,sizeof dp);
dp[1]=1;dp[0]=0;
rep(i,2,n){
dp[i]=dp[i-1]+1;
if(ok[1][i]){
dp[i]=1;
continue;
}
rep(j,1,i){
if(ok[j][i]){//不能用起点最长回文串DP
dp[i]=min(dp[i],dp[j-1]+1);
}
}
}
println(dp[n]);
}
return 0;
}

附带WA代码

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int maxn = 1e6+11;
const int oo = 0x3f3f3f3f;
const double eps = 1e-7;
typedef long long ll;
ll read(){
ll x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
char str[maxn];
int h[maxn],h2[maxn],st[maxn],dp[maxn];
int main(){
int T=read();
while(T--){
s1(str);
int n=strlen(str+1);
rep(i,1,n)st[i]=0;
rep(i,1,n){
h[i]=1;
st[i-h[i]/2]=max(st[i-h[i]/2],h[i]);
int left=i-1,right=i+1;
bool flag=0;
while(!flag&&left>0&&right<n+1){
if(str[left]==str[right]){
h[i]=max(h[i],right-left+1);
left--,right++;
st[i-h[i]/2]=max(st[i-h[i]/2],h[i]);
}else{
flag=1;
}
} }
rep(i,1,n){//中点为第i条缝
h2[i]=0;
// st[i-h2[i]/2]=max(st[i-h2[i]/2],h2[i]);
int left=i-1,right=i;
bool flag=0;
while(!flag&&left>0&&right<n+1){
if(str[left]==str[right]){
h2[i]=max(h2[i],right-left+1);
left--,right++;
st[i-h2[i]/2]=max(st[i-h2[i]/2],h2[i]);
}else{
flag=1;
} } }
memset(dp,0x3f,sizeof dp);
// rep(i,1,n) cout<<i<<" "<<h[i]<<" "<<h2[i]<<" "<<st[i]<<endl;
dp[1]=1;dp[0]=0;
rep(i,2,n){
rep(j,0,i){
if(j+st[j+1]==i){
dp[i]=min(dp[i],dp[j]+1);
}
}
}
println(dp[n]);
}
return 0;
}

UVA - 11584 DP 最少线段覆盖的更多相关文章

  1. UVA - 11584 Partitioning by Palindromes[序列DP]

    UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...

  2. uva 11584 Partitioning by Palindromes 线性dp

    // uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...

  3. UVA - 11584 划分字符串的回文串子串; 简单dp

    /** 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34398 UVA - 11584 划分字符串的回文串子串: 简单 ...

  4. 区间DP UVA 11584 Partitioning by Palindromes

    题目传送门 /* 题意:给一个字符串,划分成尽量少的回文串 区间DP:状态转移方程:dp[i] = min (dp[i], dp[j-1] + 1); dp[i] 表示前i个字符划分的最少回文串, 如 ...

  5. CODEVS3037 线段覆盖 5[序列DP 二分]

    3037 线段覆盖 5   时间限制: 3 s   空间限制: 256000 KB   题目等级 : 钻石 Diamond 题解       题目描述 Description 数轴上有n条线段,线段的 ...

  6. 【codevs3012+codevs3037】线段覆盖4+线段覆盖5(DP)

    线段覆盖4网址:http://codevs.cn/problem/3012/ 线段覆盖5网址:http://codevs.cn/problem/3037/ 题目大意:给出一条直线上的一坨线段,每条线段 ...

  7. 线段覆盖 2(序列DP)

    Code vs 3027 线段覆盖 2   题目描述 Description 数轴上有n条线段,线段的两端都是整数坐标,坐标范围在0~1000000,每条线段有一个价值,请从n条线段中挑出若干条线段, ...

  8. CodeVS 线段覆盖1~5

    #include <bits/stdc++.h> using namespace std; ; struct Info{int l,r;}P[Maxn]; int n,Cnt,F[Maxn ...

  9. codevs 3012 线段覆盖 4 & 3037 线段覆盖 5

    3037 线段覆盖 5  时间限制: 3 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 数轴上有n条线段,线段的两端都 ...

随机推荐

  1. 在Ubuntu上源码安装NodeJS

    Refer http://nqdeng.github.io/7-days-nodejs/#7.1 前提条件: 确保系统下g++版本(g++ -v)在4.6以上,python版本(python --ve ...

  2. PHP学习笔记之continue与break

    百度中有人这样解释:break是结束整个循环体,continue是结束单个循环体.昨天看燕十八老师PHP视频,讲到break,continue时,举了一个例子,理解更容易.天龙八部中,西夏国公主选婿, ...

  3. vuex 数据绑定

    操作文档: 安装vuex: cnpm install vuex --save   文档介绍: https://vuex.vuejs.org/guide/modules.html   import Vu ...

  4. Capturing ASP.NET Application Startup Exceptions

    It has become common practice to perform tasks during an ASP.NET applications start up process. Thes ...

  5. delphi TString使用(取有规律的字符串中某一项内容)

    {目的,取得下面字符串中的每一项内容,如s:='a,b,c,d',要去的用逗号隔开的每一项内容这时采用Tstring,会方便很多,TString是继承TStringList带有List所有属性.} v ...

  6. 定制JMeter取样器

    JMeter运行你区定义自己的取样器sampler,方法很简单,只需继承类org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClie ...

  7. TriggerAction扩展----ExInvokeCommandAction

    Wp&Win8中使用命令绑定时,除了Button控件自带命令绑定,其他的时候是用Interactivity库中的InvokeCommandAction实现的(Win8 需要额外安装第三方NuG ...

  8. JavaScript中function 之return false的理解(实例代码)

    1.司空见惯代码,在某一dom节点上注册事件方法 $("#btnResponse").click(Login); $("#txtCode").keydown(R ...

  9. cdq分治略解

    前言 陌上花开,可缓缓归矣                         --吴越王 寓意:意思是:田间阡陌上的花开了,你可以一边赏花,一边慢慢回来. 隐意:春天都到了,你怎么还没有回来.形容吴越王 ...

  10. Spring学习----自动装配@Resource、@Autowired、@Qualifier

    直接看下面的代码即可明白: applicationContext.xml里面添加个bean: <!--注解的注入 --> <bean id="student3" ...