UVA - 11584 DP 最少线段覆盖
题意:用最少的不可交线段覆盖整个区间,求该最小值
课上摸鱼的时候没注意到题意的转换,写了没啥卵用的回文中心最长枚举,所以代码里的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 最少线段覆盖的更多相关文章
- 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 ...
- uva 11584 Partitioning by Palindromes 线性dp
// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...
- UVA - 11584 划分字符串的回文串子串; 简单dp
/** 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34398 UVA - 11584 划分字符串的回文串子串: 简单 ...
- 区间DP UVA 11584 Partitioning by Palindromes
题目传送门 /* 题意:给一个字符串,划分成尽量少的回文串 区间DP:状态转移方程:dp[i] = min (dp[i], dp[j-1] + 1); dp[i] 表示前i个字符划分的最少回文串, 如 ...
- CODEVS3037 线段覆盖 5[序列DP 二分]
3037 线段覆盖 5 时间限制: 3 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 数轴上有n条线段,线段的 ...
- 【codevs3012+codevs3037】线段覆盖4+线段覆盖5(DP)
线段覆盖4网址:http://codevs.cn/problem/3012/ 线段覆盖5网址:http://codevs.cn/problem/3037/ 题目大意:给出一条直线上的一坨线段,每条线段 ...
- 线段覆盖 2(序列DP)
Code vs 3027 线段覆盖 2 题目描述 Description 数轴上有n条线段,线段的两端都是整数坐标,坐标范围在0~1000000,每条线段有一个价值,请从n条线段中挑出若干条线段, ...
- CodeVS 线段覆盖1~5
#include <bits/stdc++.h> using namespace std; ; struct Info{int l,r;}P[Maxn]; int n,Cnt,F[Maxn ...
- codevs 3012 线段覆盖 4 & 3037 线段覆盖 5
3037 线段覆盖 5 时间限制: 3 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 数轴上有n条线段,线段的两端都 ...
随机推荐
- The 'Microsoft Jet OLEDB 4.0 Provider' is not registered on the local machine
在一台Win7 64位的操纵系统上部署的C# Web系统,操作Excel,批量导入数据,报错,提示错误信息: The ‘Microsoft Jet OLEDB 4.0 Provider' is not ...
- 面试题:JVM类加载机制详解(一)JVM类加载过程 背1
首先Throws(抛出)几个自己学习过程中一直疑惑的问题: 1.什么是类加载?什么时候进行类加载? 2.什么是类初始化?什么时候进行类初始化? 3.什么时候会为变量分配内存? 4.什么时候会为变量赋默 ...
- CSS文本溢出处理
1.超出层的高度和宽度时文本自动隐藏 overflow:hidden;text-overflow:ellipsis; 2.超出层的宽度时隐藏溢出的文本以...表示,Firefox不兼容,只隐藏溢出的文 ...
- Luogu 4091 [HEOI2016/TJOI2016]求和
BZOJ 4555 一道模板题. 第二类斯特林数有公式: $$S(n, m) = \frac{1}{m!}\sum_{i = 0}^{m}(-1)^i\binom{m}{i}(m - i)^n$$ 考 ...
- mybatis 获得一个map的返回集合
在使用mybatis 查询结果集,有时会有需求返回一个map比如表 id username 1 name1 2 name2 3 name3 希望的查询结果是一个map 并且以id为key 表为实体 ...
- VUE实战项目-数据转换之道
前言 公司的这个项目从去年底启动.至今经历winform版本与当前的VUE两个版本,前后经历不足3个月的时间.从纯技术角度来看,推进速度都很优异.究其原因,大抵我们都是喜欢“偷懒”的程序员,把能封装. ...
- python2.7响应数据中unicode转中文
print ("响应结果:%s" % r.content.decode('unicode_escape')) 一. 在爬虫抓取网页信息时常需要将类似"\u4eba\u75 ...
- MVC中的Controllers和View分别放到单独的项目中
将Controllers放到独立项目中: 第一步:创建Mvc.Controllers,Mvc.Models和UI三个项目 Mvc.Controllers用来编写Controllers Mvc.Mode ...
- [.net 多线程]ThreadPool的安全机制
ThreadPool类,有两个方法我们没有用到,UnsafeQueueUserWorkItem 和UnsafeRegisterWaitForSingleObject. 为了完全理解这些方法,首先,我们 ...
- SQL 全角半角转换-(摘抄)
/****** SQL转换全角/半角函数 开始******/ CREATE FUNCTION ConvertWordAngle ( @str NVARCHAR(4000), --要转换的字符串 @fl ...