# bzoj1090[SCOI2003]字符串折叠

## Sample Input

NEERCYESYESYESNEERCYESYESYES

14

# 代码

 1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<cstdlib>
5 #include<algorithm>
6 #include<cmath>
7 using namespace std;
8 char ch[110];
9 int dp[110][110];
10 bool used[110][110];
11 int ans;
12 bool can(int l,int r,int fl,int fr){
13     if((r-l+1)%(fr-fl+1)!=0)  return false;
14     int i,j;
15     for(i=l;i<=r;++i){
16         if(ch[i]!=ch[(i-l)%(fr-fl+1)+fl])  return false;
17     }
18     return true;
19 }
20 int wei(int x){
21     int a=0;
22     while(x){
23         a++;x=x/10;
24     }
25     return a;
26 }
27 int dfs(int l,int r){
28     if(l==r)  return 1;
29     if(used[l][r])  return dp[l][r];
30     used[l][r]=1;
31     int an=r-l+1;
32     int i,j;
33     for(i=l;i<r;++i){
34         an=min(an,dfs(l,i)+dfs(i+1,r));
35         if(can(i+1,r,l,i)){
36             an=min(an,dfs(l,i)+2+wei((r-i)/(i-l+1)+1));
37         }
38     }
39     dp[l][r]=an;
40     return dp[l][r];
41 }
42 int main(){
43     scanf("%s",ch);
44     int len=strlen(ch);
45     memset(used,0,sizeof(used));
46     ans=dfs(0,len-1);
47     printf("%d\n",ans);
48     return 0;
49 }

posted @ 2018-06-11 17:59  lazytear  阅读(55)  评论(0编辑  收藏