提交时间:2022-07-13 11:53:01
运行 ID: 51567
#include<cstdio> #include<cstring> #define rep(a,b,c) for(int c(a);c<=(b);++c) #define drep(a,b,c) for(int c(a);c>=(b);--c) inline int read() { int res=0;char ch=getchar();while(ch<'0'||ch>'9')ch=getchar(); while(ch<='9'&&ch>='0')res=res*10+(ch^48),ch=getchar();return res; } inline short Get(){char ch=getchar();while(ch<'a'||ch>'z')ch=getchar();return ch-'a';} template<typename T> inline T max(const T&x,const T&y){return x<y?y:x;} const int N=3010,INF=0x3f3f3f3f; int a[N],dp[N][1<<12|7],ans,lst[N][13],K,n,typ,stk[N],top; inline void dfs(int i,int S,int ans) { if(!i)return;putchar(a[i]+'a'); int T=S^(1<<a[i]); for(int j=0;j<K;++j) { if(j==a[i]) { if(dp[lst[i-1][a[i]]][S]==ans-1) { dfs(lst[i-1][a[i]],S,ans-1); return; } } else if(T>>j&1) { if(dp[i][S]==dp[lst[i-1][j]][T]+1) { dfs(lst[i-1][j],T,ans-1); return; } } } } int main() { // freopen("seq.in","r",stdin); // freopen("seq.out","w",stdout); n=read();K=read();typ=read();rep(1,n,i)a[n-i+1]=Get(); rep(1,n,i) { memcpy(lst[i],lst[i-1],sizeof(lst[i])); for(int S=0;S<(1<<K);++S)if(S>>a[i]&1) { dp[i][S]=dp[lst[i][a[i]]][S]+1; int T=S^(1<<a[i]); for(int j=0;j<K;++j)if(T>>j&1) { dp[i][S]=max(dp[i][S]+0,dp[lst[i][j]][T]+1); } ans=max(ans,dp[i][S]); } lst[i][a[i]]=i; } printf("%d\n",ans);if(!typ)return 0; for(int c=0;c<K;++c) { drep(n,1,i)if(a[i]==c) { for(int S=0;S<(1<<K);++S)if(S>>c&1) { if(dp[i][S]==ans) { dfs(i,S,ans); return 0; } } } } return 0; }