Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
119113 陈家宝 最优子序列 C++ 通过 100 364 MS 48536 KB 1629 2024-01-04 12:54:06

Tests(20/20):


#include<bits/stdc++.h> #define rep(a,b,c) for(int c(a);c<=(b);++c) #define drep(a,b,c) for(int c(a);c>=(b);--c) using namespace std; 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'; } const int N=3010,INF=0x3f3f3f3f; int a[N],dp[N][1<<12|7],ans,lst[N][13],K,n,typ,stk[N],top; string res;inline void dfs(int i,int S,int ans) { if(!i)return; res+=(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[lst[i-1][j]][T]==ans-1){ dfs(lst[i-1][j],T,ans-1); return; } } } } int main() { 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){ string s; for(int S=0;S<(1<<K);++S)if(S>>c&1){ if(dp[i][S]==ans){ res.clear(); dfs(i,S,ans); if(!s.size())s=res; else s=min(s,res); } } if(s.size()){ cout<<s; return 0; } } } } return 0; }


测评信息: