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

课上摸鱼的时候没注意到题意的转换,写了没啥卵用的回文中心最长枚举,所以代码里的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. lucene 第一天

    Lucene/Solr   第一天 1. 课程计划 Lucene介绍 全文检索流程介绍 a) 索引流程 b) 搜索流程 Lucene入门程序 a) 索引实现 b) 搜索实现 分词器 a) 分词介绍 b ...

  2. 面试题:Java开发中的23种设计模式详解(转)

    设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

  3. github如何上传代码

    别人写的太好了,没必要重写.备份给自己参看. 1.https://www.cnblogs.com/zlxbky/p/7727895.html 2.https://blog.csdn.net/pql92 ...

  4. 如何在Django模型中管理并发性 orm select_for_update

    如何在Django模型中管理并发性 为单用户服务的桌面系统的日子已经过去了 - 网络应用程序现在正在为数百万用户提供服务,许多用户出现了广泛的新问题 - 并发问题. 在本文中,我将介绍在Django模 ...

  5. <abbr> 元素的样式为显示在文本底部的一条虚线边框,当鼠标悬停在上面时会显示完整的文本(只要您为 <abbr> title 属性添加了文本)

    <abbr title="World Wide Web">WWW</abbr><br><abbr title="Real Sim ...

  6. easyui-dialog 弹窗

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  7. Java 核心类库之反射机制

    1:什么是反射机制? 2:反射机制它可以做什么呢? 3:反射机制对应的API又是什么? 1):通过反射机制来获取一个对象的全限定名称(完整包名),和类名: 2):实例化Class对象 3):获取对象的 ...

  8. Java String对象面试题分析

  9. (转)正则表达式—RegEx(RegularExpressio)(三)

    原文地址:http://www.cnblogs.com/feng-c-x/archive/2013/09/05/3302465.html 今日随笔,继续写一点关于正则表达式的 知识.前两天介绍了正则表 ...

  10. MongoDB整理笔记のReplica oplog

    主从操作日志oplog MongoDB的Replica Set架构是通过一个日志来存储写操作的,这个日志就叫做"oplog".oplog.rs是一个固定长度的capped coll ...