区间DP+next求循环节 uva 6876

时间:2021-01-10 19:16:55
 // 区间DP+next求循环节 uva 6876
// 题意:化简字符串 并表示出来
// 思路:dp[i][j]表示 i到j的最小长度
// 分成两部分 再求一个循环节 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
using namespace std;
#define LL long long
typedef pair<int,int> pii;
const int inf = 0x3f3f3f3f;
const int MOD = ;
const int N = ;
const int maxx = ;
#define clc(a,b) memset(a,b,sizeof(a))
const double eps = 0.025;
void fre() {freopen("in.txt","r",stdin);}
void freout() {freopen("out.txt","w",stdout);}
inline int read() {int x=,f=;char ch=getchar();while(ch>''||ch<'') {if(ch=='-') f=-; ch=getchar();}while(ch>=''&&ch<='') {x=x*+ch-'';ch=getchar();}return x*f;} char s[N];
int dp[N][N];
int cas=;
int nextt[N]; int getnext(char *t,int len){
int i=,j=-;
// int len=strlen(t);
nextt[]=-;
while(i<len){
if(t[i]==t[j]||j==-){
i++;
j++;
nextt[i]=j;
}
else
j=nextt[j];
}
return len-nextt[len];
} int getbit(int x){
int cnt=;
while(x){
x/=;
cnt++;
}
return cnt;
}
void DP(){
int n=strlen(s+);
for(int i=;i<=n;i++) {
for(int j=i+;j<=n;j++)
dp[i][j]=;
dp[i][i]=;
}
for(int len=;len<=n;len++){
for(int l=;l+len-<=n;l++){
int r=l+len-;
for(int k=l;k<r;k++){
dp[l][r]=min(dp[l][k]+dp[k+][r],dp[l][r]);
}
int t=getnext(s+l,len);
if((len%t)==){
int tem=dp[l][l+t-];
if(t>) tem+=;
tem+=getbit(len/t);
dp[l][r]=min(dp[l][r],tem);
}
}
}
printf("Case %d: %d\n",cas++,dp[][n]);
}
int main(){
// fre();
while(~scanf("%s",s+)){
if(s[]=='') break;
DP();
}
return ;
}