提交时间:2022-07-13 12:22:11

运行 ID: 51642

#include<cstdio> #include<iostream> #include<cstring> using namespace std; const int N=3005; int n,k,type,ans,st[N],top; int dp[N][4500][2],lst[13]; pair<int,int>pre[N][4500][2]; char s[N],a[N],b[N]; int main() { // freopen("seq.in","r",stdin); // freopen("seq.out","w",stdout); scanf("%d%d%d",&n,&k,&type); scanf("%s",s+1); for(int i=1; i<=n; i++) { for(int w=0; w<(1<<k); w++) { if(w&(1<<(s[i]-'a'))) { if(dp[i][w][1]<dp[lst[s[i]-'a']][w][1]+1) dp[i][w][1]=dp[lst[s[i]-'a']][w][1]+1,pre[i][w][1]=make_pair(lst[s[i]-'a'],w); } else { if(dp[i-1][w][0]+1>dp[i][w|(1<<(s[i]-'a'))][1]) dp[i][w|(1<<(s[i]-'a'))][1]=dp[i-1][w][0]+1,pre[i][w|(1<<(s[i]-'a'))][1]=pre[i-1][w][0]; if(dp[i-1][w][1]+1>dp[i][w|(1<<(s[i]-'a'))][1]) dp[i][w|(1<<(s[i]-'a'))][1]=dp[i-1][w][1]+1,pre[i][w|(1<<(s[i]-'a'))][1]=make_pair(i-1,w); } if(dp[i][w][0]<dp[i-1][w][0]) dp[i][w][0]=dp[i-1][w][0],pre[i][w][0]=pre[i-1][w][0]; if(dp[i][w][0]<dp[i-1][w][1]) dp[i][w][0]=dp[i-1][w][1],pre[i][w][0]=make_pair(i-1,w); } lst[s[i]-'a']=i; } for(int i=1; i<=n; i++) for(int w=0; w<(1<<k); w++) ans=max(ans,dp[i][w][1]); printf("%d\n",ans); if(type) { for(int i=1; i<=n; i++) { for(int w=0; w<(1<<k); w++) { if(dp[i][w][1]==ans) { int x=i,y=w,p,q; while(x) { st[++top]=x; p=pre[x][y][1].first; q=pre[x][y][1].second; x=p; y=q; } break; } } if(top) break; } while(top) printf("%c",s[st[top--]]); } fclose(stdin); fclose(stdout); return 0; }