bzoj1090 [SCOI2003]字符串折叠
区间DP就是好写
dp[i][j]表示i~j折叠后的最短长度
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 void inin(int &ret) 27 { 28 ret=0;int f=0;char ch=getchar(); 29 while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();} 30 while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar(); 31 ret=f?-ret:ret; 32 } 33 char s[111]; 34 int dp[111][111],n; 35 int js(int x) 36 { 37 int ret=0; 38 while(x)x/=10,ret++; 39 return ret; 40 } 41 bool pd(int l,int r,int ll,int rr) 42 { 43 if((rr-ll+1)%(r-l+1))return 0; 44 re(i,ll,rr)if(s[i]!=s[(i-ll)%(r-l+1)+l])return 0; 45 return 1; 46 } 47 int dfs(int l,int r) 48 { 49 int &ret=dp[l][r]; 50 if(ret)return ret; 51 ret=r-l+1; 52 re(i,l,r-1)ret=min(ret,dfs(l,i)+dfs(i+1,r)); 53 re(i,l,l+((r-l+1)>>1))if(pd(l,i,i+1,r))ret=min(ret,dfs(l,i)+2+js((r-l+1)/(i-l+1))); 54 return ret; 55 } 56 int main() 57 { 58 strin(s+1); 59 n=strlen(s+1); 60 printf("%d",dfs(1,n)); 61 return 0; 62 }