BZOJ 1068 [SCOI2007]压缩

1068: [SCOI2007]压缩

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 667  Solved: 417
[Submit][Status][Discuss]

Sample Input

bcdcdcdcdxcdcdcdcd

12

HINT

【限制】

100%的数据满足：1<=n<=50 100%的数据满足：1<=n<=50

Source

 1 #include<iostream>
2 #include<cstdio>
3 #include<cmath>
4 #include<algorithm>
5 #include<stack>
6 #include<queue>
7 #include<cstring>
8 #define PAU putchar(' ')
9 #define ENT putchar('\n')
10 #define MSE(a,b) memset(a,b,sizeof(a))
11 #define REN(x) for(ted*e=fch[x];e;e=e->nxt)
12 #define TIL(x) for(int i=1;i<=x;i++)
13 #define ALL(x) for(int j=1;j<=x;j++)
14 using namespace std;
15 const int maxn=50+10,inf=1e8;
16 char s[maxn];int f[maxn][maxn][2],n;
17 bool same(int L,int R){
18     if((R-L+1)&1)return false;int M=R-L+1>>1;
19     for(int i=L;i<L+M;i++)if(s[i]!=s[i+M])return false;return true;
20 }
21 int solve(int L,int R,bool t){
22     if(L==R)return 1;if(f[L][R][t])return f[L][R][t];int&res=f[L][R][t];res=inf;
23     if(t)for(int i=L;i<R;i++)res=min(res,1+solve(L,i,1)+solve(i+1,R,1));
24     for(int i=L;i<R;i++)res=min(res,solve(L,i,t)+R-i);
25     if(same(L,R))res=min(res,solve(L,L+R>>1,0)+1);return res;
26 }
28     int x=0;bool sig=true;char ch=getchar();
29     for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=false;
30     for(;isdigit(ch);ch=getchar())x=10*x+ch-'0';return sig?x:-x;
31 }
32 inline void write(int x){
33     if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
34     int len=0;static int buf[20];while(x)buf[len++]=x%10,x/=10;
35     for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
36 }
37 int main(){
38     scanf("%s",s+1);n=strlen(s+1);write(solve(1,n,1));return 0;
39 }

posted @ 2015-08-19 16:54  AI_Believer  阅读(...)  评论(...编辑  收藏