Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
51641 Ender 最优子序列 C++ 运行超时 10 1000 MS 31744 KB 1157 2022-07-13 12:22:11

Tests(2/20):


#include <iostream> #include <cstdio> using namespace std; int f[3001][4097],last[13],a[21],n,K,ans; bool ap[14]; string s; int solve() { int i,co = 0; char last = 0; for(i = 0;i <= K;i++) ap[i] = 0; for(i = 1;i <= n;i++) { if(a[i]) { if(!ap[s[i] - 'a']) { last = s[i]; ap[s[i] - 'a'] = 1; co++; } else { if(last == s[i]) co++; else return 0; } } } return co; } void DFS(int k) { if(k == n + 1) { ans = max(ans,solve()); return; } a[k] = 0; DFS(k + 1); a[k] = 1; DFS(k + 1); return; } int main() { int type,i,j,k; cin>>n>>K>>type>>s; s = " " + s; if(n <= 20) { DFS(1); cout<<ans<<endl; return 0; } for(i = 1;i <= n;i++) { int x = s[i] - 'a' + 1; for(j = 1;j <= (1<<K) - 1;j++) { if((1<<(x - 1))&j) f[i][j] = max(f[i][j],max(f[last[x]][j - (1<<(x - 1))] + 1,f[last[x]][j] + 1)); else { for(k = 1;k < i;k++) { if(s[k] == s[i]) continue; f[i][j] = max(f[i][j],f[k][j] + 1); } } ans = max(ans,f[i][j]); } last[x] = i; } cout<<ans<<endl; return 0; }


测评信息: