提交时间:2022-07-13 16:37:15
运行 ID: 51837
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int N=3e3+10; typedef long long ll; ll f[N][(1<<13)+1]; int suf[N][(1<<13)+1],suff[N][(1<<13)+1]; int n,k,type; char ch[N]; int lst[N][14]; void print(int i,int j) { if(i>n) return ; putchar(ch[i]); print(suf[i][j],suff[i][j]); } int main() { scanf("%d %d %d",&n,&k,&type); scanf("%s",ch+1); for(int i=1;i<=n+1;i++) { for(int j=0;j<k;j++) lst[i][j]=lst[i-1][j]; lst[i][ch[i-1]-'a']=i-1; } for(int i=n+1;i>=1;i--) { for(int s=0;s<(1<<k);s++) { for(int j=0;j<k;j++) { if(!lst[i][j]) continue; if(ch[lst[i][j]]==ch[i]) { if(f[i][s]+1>f[lst[i][j]][s]) { f[lst[i][j]][s]=f[i][s]+1; suf[lst[i][j]][s]=i,suff[lst[i][j]][s]=s; } else if(f[i][s]+1==f[lst[i][j]][s]&&ch[i]<ch[suf[lst[i][j]][s]]) suf[lst[i][j]][s]=i,suff[lst[i][j]][s]=s; } else if((s&(1<<j))==0) { if(f[i][s]+1>f[lst[i][j]][s|(1<<(ch[lst[i][j]]-'a'))]) { f[lst[i][j]][s|(1<<(ch[lst[i][j]]-'a'))]=f[i][s]+1; suf[lst[i][j]][s|(1<<(ch[lst[i][j]]-'a'))]=i,suff[lst[i][j]][s|(1<<(ch[lst[i][j]]-'a'))]=s; } else if(f[i][s]+1==f[lst[i][j]][s|(1<<(ch[lst[i][j]]-'a'))]&&ch[i]<ch[suf[lst[i][s|(1<<(ch[lst[i][j]]-'a'))]][s]]) suf[lst[i][j]][s|(1<<(ch[lst[i][j]]-'a'))]=i,suff[lst[i][j]][s|(1<<(ch[lst[i][j]]-'a'))]=s; } } } } ll ans=0,nowi=0,nowj=0; for(int i=1;i<=n+1;i++) for(int s=0;s<(1<<k);s++) if(f[i][s]>ans) ans=f[i][s],nowi=i,nowj=s; printf("%lld\n",ans); if(type) print(nowi,nowj); return 0; }