Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
51574 | tengzifan | 最优子序列 | C++ | 运行超时 | 30 | 1000 MS | 466232 KB | 1786 | 2022-07-13 11:53:25 |
#include <bits/stdc++.h> using namespace std; int n,k,type; int dp[3005][4096+5]; //string s1[15]={"a","b","c","d","e","f","g","h","i","j","k","l"}; int d1[15]= {1,2,4,8,16,32,64,128,256,512,1024,2048}; string s,str[3005][4096+5]; bool com(string s1,string s2) { int len1=s1.size(); int len2=s2.size(); for(int i=0; i<min(len1,len2); ++i) { if(s1[i] > s2[i]) return false; if(s1[i] < s2[i]) return true; } return len1<len2; } int main() { //freopen("seq.in","r",stdin); //freopen("seq.out","w",stdout); scanf("%d%d%d",&n,&k,&type); cin>>s; for(int i=n; i>=1; --i) s[i]=s[i-1]; s[0]='*'; for(int i=1; i<=n; ++i) { dp[i][0]=1; str[i][0]=s[i]; } for(int i=1; i<=n; ++i) { for(int S=0; S<(1<<k); ++S) { for(int j=i+1; j<=n; ++j) { if(dp[i][S]==0) continue; if((S & d1[s[j]-'a'])!=0) continue; else { int SS; if(str[i][S][dp[i][S]-1] == s[j]) SS=S; else SS=S+d1[str[i][S][dp[i][S]-1] - 'a']; //cout<<str[i][S][dp[i][S]-1]<<endl; if(dp[i][S]+1 > dp[j][SS]) { dp[j][SS]=dp[i][S]+1; str[j][SS]=str[i][S]+s[j]; } else if(dp[i][S]+1 == dp[j][SS]) { string ss=str[i][S]+s[j]; if(com(ss,str[j][SS])) str[j][SS]=ss; } } } } } int ans=0; string anstring; for(int S=0; S<(1<<k); ++S) { if(dp[n][S] > ans) { ans=dp[n][S]; anstring = str[n][S]; } else if(dp[n][S] == ans) { if(com(str[n][S],anstring)) anstring = str[n][S]; } } printf("%d\n",ans); if(type != 0) cout<<anstring; return 0; }