玄学dp,感觉我写的复杂度好高的样子,但是直接水过啦~
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
typedef long long LL;
inline LL read()
{
LL x=0;bool f=0;char c=getchar();
for (;c<'0'||c>'9';c=getchar()) f=c=='-'?1:0;
for (;c>='0'&&c<='9';c=getchar()) x=x*10+c-'0';
return f?-x:x;
}
const int N=110;
int n,f[N][N],lca[N][N],bt[N];char str[N];
int dfs(int l,int r)
{
if (l>r) return 0;
if (f[l][r]) return f[l][r];
if (l==r) return f[l][r]=1;
int rec=r-l+1;
for (int i=l,cnt;i<=r;i++)
for (int j=1,k;j<=r-i;j++)
{
cnt=1;
for (k=i+j;k+j-1<=r&&lca[i][k]>=j;k+=j,cnt++);
rec=min(rec,dfs(l,i-1)+bt[cnt]+2+dfs(i,i+j-1)+dfs(k,r));
}
return f[l][r]=rec;
}
int main()
{
scanf("%s",str),n=strlen(str);
for (int i=1;i<=9;i++) bt[i]=1;
for (int i=10;i<=99;i++) bt[i]=2;
bt[100]=3;
for (int i=n-1;~i;i--)
for (int j=n-1;j>=i;j--)
lca[i][j]=(str[i]==str[j])?lca[i+1][j+1]+1:0;
printf("%d\n",dfs(0,n-1));
return 0;
}