bzoj1068 [SCOI2007]压缩

题目链接

还是区间DP做起来舒服些

dp[i][j][k]表示i~j前有/没有'W'的最短长度

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<string>
 7 #include<cmath>
 8 #include<ctime>
 9 #include<queue>
10 #include<stack>
11 #include<map>
12 #include<set>
13 #define rre(i,r,l) for(int i=(r);i>=(l);i--)
14 #define re(i,l,r) for(int i=(l);i<=(r);i++)
15 #define Clear(a,b) memset(a,b,sizeof(a))
16 #define inout(x) printf("%d",(x))
17 #define douin(x) scanf("%lf",&x)
18 #define strin(x) scanf("%s",(x))
19 #define LLin(x) scanf("%lld",&x)
20 #define op operator
21 #define CSC main
22 typedef unsigned long long ULL;
23 typedef const int cint;
24 typedef long long LL;
25 using namespace std;
26 cint inf=214748364;
27 void inin(int &ret)
28 {
29     ret=0;int f=0;char ch=getchar();
30     while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();}
31     while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar();
32     ret=f?-ret:ret;
33 }
34 int n,hh[55][55][2];
35 char s[55];
36 bool pd(int l,int r)
37 {
38     int len=(r-l+1);
39     if(len%2)return 0;
40     re(i,1,len>>1)if(s[l+i-1]!=s[(len>>1)+i+l-1])return 0;
41     return 1;
42 }
43 int dfs(int l,int r,int bo)
44 {
45     int &ret=hh[l][r][bo];
46     if(ret!=inf)return ret;
47     ret=r-l+1;
48     if(bo)re(i,l,r-1)ret=min(ret,dfs(l,i,1)+dfs(i+1,r,1)+1);
49     re(i,l,r-1)ret=min(ret,dfs(l,i,bo)+r-i);
50     if(pd(l,r))ret=min(ret,dfs(l,(l+r)>>1,0)+1);
51     return ret;
52 }
53 int main()
54 {
55     strin(s+1);
56     n=strlen(s+1);re(i,1,n)re(j,1,n)re(k,0,1)hh[i][j][k]=inf;
57     printf("%d",dfs(1,n,1));
58      return 0;
59 }

 

posted @ 2016-03-12 16:31  HugeGun  阅读(...)  评论(...编辑  收藏