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条线段,线段的两端都 ...
随机推荐
- Linux内核的特征
Linux内核的特征 Linux是个人计算机和工作站上的Unix类操作系统.但是,它绝不是简化的Unix.相反,Linux是强有力和具有创新意义的Unix类操作系统.它不仅继承了Unix的特征,而且在 ...
- Luogu 4841 城市规划
BZOJ 3456 权限题 太菜了推不出式子 我们设$f(n)$表示$n$个点的无向连通图的数量,那么有 $$f(n) = 2^{\binom{n}{2}} - \sum_{i = 1}^{n - 1 ...
- oracle 序列初始化的plsql块脚本
declare seq_name dba_sequences.SEQUENCE_NAME%TYPE; cursor mycur is select * from dba_sequences where ...
- Which Uri Encoding method should i use in C#/.net?
June 19, 2015 This too is one of the boring "factual" posts. Sorry Lachlan. I never know w ...
- (转)【推荐】使用Jquery+EasyUI进行框架项目开发案例讲解之一---员工管理源码分享
原文地址:http://www.cnblogs.com/huyong/p/3334848.html 在开始讲解之前,我们先来看一下什么是Jquery EasyUI?jQuery EasyUI是一组基于 ...
- 百度图片API
转载请注明出处:http://blog.csdn.net/yuanwofei/article/details/16343743 一.通用api http://image.baidu.com/i?tn= ...
- [.net 多线程]AutoResetEvent, ManualResetEvent
ManualResetEvent: 通知一个或多个正在等待的线程已发生事件,允许线程通过发信号互相通信,来控制线程是否可心访问资源. Set() : 用于向 ManualResetEvent 发送信号 ...
- C#中return的两个作用
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- WinForm中DataGridView的使用(六) - 特殊处理的小地方
列标题不能居中的解决方法 一般列标题的居中我们都使用this.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignmen ...
- loj #6121. 「网络流 24 题」孤岛营救问题
#6121. 「网络流 24 题」孤岛营救问题 题目描述 1944 年,特种兵麦克接到国防部的命令,要求立即赶赴太平洋上的一个孤岛,营救被敌军俘虏的大兵瑞恩.瑞恩被关押在一个迷宫里,迷宫地形复杂, ...