提交时间:2022-07-13 11:50:14
运行 ID: 51518
#include <bits/stdc++.h> using namespace std; string a, b; char s[3005]; int n, k, typ, mx=1, ret, flag; struct ans { int id, num, ltr[15]; } tmp; struct node { int lst, f=1, ltr[15]; } t[3005]; queue<ans> q; inline int mn(int xx, int yy) { return xx<yy ? xx:yy; } int main() { scanf("%d%d%d%s", &n, &k, &typ, s+1); for(int i=1; i<=n; i++) { t[i].ltr[s[i]-'a'+1] = 1; for(int j=1; j<i; j++) { if((t[j].ltr[s[i]-'a'+1]==0||s[i]==s[j]) && t[j].f+1>=t[i].f) { t[i] = t[j]; t[i].lst = j; t[i].f++; t[i].ltr[s[i]-'a'+1]++; } else if(t[j].f+1-t[j].ltr[s[i]-'a'+1] >= t[i].f) { t[i] = t[j]; t[i].lst = j; t[i].f = t[j].f+1-t[j].ltr[s[i]-'a'+1]; t[i].ltr[s[i]-'a'+1] = 1; } if(t[i].f > mx) { mx = t[i].f; while(!q.empty()) q.pop(); } if(t[i].f == mx) { tmp.id = i, tmp.num = t[i].f; for(int z=1; z<=k; z++) tmp.ltr[z] = t[i].ltr[z]; q.push(tmp); } } } printf("%d\n", mx); if(typ == 0) return 0; tmp = q.front(); while(tmp.id >= 1) { if(tmp.ltr[s[tmp.id]-'a'+1]) { a = s[tmp.id]+a; tmp.ltr[s[tmp.id]-'a'+1]--; } tmp.id = t[tmp.id].lst; } if(typ == 2) { q.pop(); while(!q.empty()) { tmp = q.front(); while(tmp.id >= 1) { if(tmp.ltr[s[tmp.id]-'a'+1]) { b = s[tmp.id]+b; tmp.ltr[s[tmp.id]-'a'+1]--; } tmp.id = t[tmp.id].lst; } q.pop(); flag = 0, ret = mn(a.size(), b.size()); for(int i=1; i<=ret; i++) { if(a[i] == b[i]) continue; else if(a[i] < b[i]) { flag = 1; } else if(a[i] > b[i]) flag = 2; break; } if(flag == 0) flag = (a.size()<=b.size() ? 1:2); if(flag == 2) a = b; } } cout << a; return 0; }