Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
51620 heaksicn 最优子序列 C++ 解答错误 40 262 MS 384632 KB 2018 2022-07-13 12:18:35

Tests(8/20):


#include<bits/stdc++.h> #define ll long long #define mp make_pair #define pii pair<int,int> #define fi first #define se second using namespace std; inline int read(){ int X=0,w=0; char ch=0; while(!isdigit(ch)) {w|=ch=='-';ch=getchar();} while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar(); return w?-X:X; } inline void write(int x){ if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } int n,k,ty; char s[3001]; int cnt; int f[3001][4100]; int g[3001][4100]; int lst[26]; pii asf[3001][4100]; pii asg[3001][4100]; int st[3001],top; int main(){ // freopen("seq4.in","r",stdin); // freopen("test.txt","w",stdout); n=read(),k=read(),ty=read(); cin>>s+1; for(int i=1;i<=n;i++){ for(int S=0;S<(1<<k);S++){ if(S&(1<<(s[i]-'a'))){ if(f[i][S]<f[lst[s[i]-'a']][S]+1){ asf[i][S]=mp(lst[s[i]-'a'],S); } f[i][S]=max(f[i][S],f[lst[s[i]-'a']][S]+1); } else{ if(f[i][S|(1<<(s[i]-'a'))]<f[i-1][S]+1){ asf[i][S|(1<<(s[i]-'a'))]=mp(i-1,S); } if(f[i][S|(1<<(s[i]-'a'))]<g[i-1][S]+1){ asf[i][S|(1<<(s[i]-'a'))]=asg[i-1][S]; } f[i][S|(1<<(s[i]-'a'))]=max(f[i][S|(1<<(s[i]-'a'))],(f[i-1][S],g[i-1][S])+1); } if(g[i][S]<f[i][S]) asg[i][S]=mp(i-1,S); if(g[i][S]<g[i-1][S]) asg[i][S]=asg[i-1][S]; g[i][S]=max(g[i][S],f[i][S]); g[i][S]=max(g[i][S],g[i-1][S]); } lst[s[i]-'a']=i; } int ans=0; for(int i=1;i<=n;i++){ for(int S=0;S<(1<<k);S++){ ans=max(ans,f[i][S]); ans=max(ans,g[i][S]); } } write(ans); puts(""); if(ty){ for(int i=1;i<=n;i++){ for(int S=(1<<k)-1;S>=0;S--){ if(f[i][S]==ans){ int ls1=i,ls2=S; while(ls1>=1){ st[++top]=ls1; int nw1=asf[ls1][ls2].fi; int nw2=asf[ls1][ls2].se; // cout<<asf[ls1][ls2].fi<<" "<<asg[ls1][ls2].fi<<endl; ls1=nw1; ls2=nw2; } } } } puts(""); for(int i=top;i>=1;i--) cout<<s[st[i]]; } return 0; }


测评信息: