提交时间:2022-07-13 12:20:09
运行 ID: 51630
#include <bits/stdc++.h> using namespace std; char c[3005]; int p[3005]; int f[9005][3005]; int last[26]; int g[9005][3005]; string s[9005][3005]; int n,k,type; void pre() { for(int i=1; i<=n; i++) { int num=c[i]-'a'+1; if(!last[num]) continue; if(last[num]==i) p[i]=0; else p[i]=last[num]; last[num]=i; } } int tp[26]; int ts; int main() { scanf("%d%d%d",&n,&k,&type); getchar(); for(int i=1; i<=n; i++) { c[i]=getchar(); int num=c[i]-'a'+1; if(!last[num]) last[num]=i,tp[num]=++ts; } pre(); for(int i=1; i<1<<(ts+1); i++) for(int j=1; j<=n; j++) { int num=tp[c[j]-'a'+1]; if(1<<num&i) { f[i][j]=f[i-(1<<num)][j-1]+1; g[i][j]=j-1; s[i][j]=s[i-(1<<num)][j-1]+c[j]; int tmp=f[i][p[j]]+1; if(tmp>f[i][j]) f[i][j]=tmp,g[i][j]=p[j],s[i][j]=s[i][p[j]]+c[j]; else { if(tmp==f[i][j]&&s[i][j-1]<s[i-(1<<num)][p[j]]) { g[i][j]=p[j]; s[i][j]=s[i][p[j]]+c[j]; } } } else f[i][j]=f[i][j-1],g[i][j]=g[i][j-1],s[i][j]=s[i][j-1]; } int maxn=0,maxi; string ans; int k=(1<<(ts+1))-1; for(int i=1; i<=n; i++) if(f[k][i]>maxn) maxn=f[k][i],ans=s[k][i]; else if(maxn&&f[k][i]==maxn&&s[k][i]<ans) ans=s[k][i]; printf("%d\n",maxn); if(type) printf("%s\n",ans.c_str()); return 0; }