题解:
其实就是对应三种dp的转移方式
1、拼接类型
dp[i][j] = dp[i][c] + dp[c][j]
2、不变类型
dp[i][j] = j-i+1
3、重复类型(必须满足有k个循环节)
dp[i][j] = width(k) + 2 + dp[i][i+L-1]
直接记忆化搜索即可,复杂度n^3logn(枚举循环节近似为logn)
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char S[];
int dp[][]; int width(int x){
int l = ;
while(x) { l++; x /= ; }
return l;
} int dfs(int i, int j){
if(i > j) return ;
if(dp[i][j] < ) return dp[i][j];
int L = j-i+;
if(L == ) return ;
dp[i][j] = L;
for(int c = i; c < j; c++) dp[i][j] = min(dp[i][j], dfs(i, c) + dfs(c+, j));
for(int k = ; k <= L; k++){
if(L % k != ) continue;
int l = L/k, f = ;
for(int t = ; t < k- && !f; t++){
for(int c = ; c < l && !f; c++)
if(S[i+l*t+c] != S[i+l*(t+)+c]) f = ;
}
if(!f) dp[i][j] = min(dp[i][j], width(k)++dfs(i, i+l-));
}
return dp[i][j];
} int main()
{
int T;
cin>>T;
while(T--){
cin>>S;
int n = strlen(S);
memset(dp, , sizeof(dp));
cout<<dfs(, n-)<<endl;
}
return ;
}