提交时间:2022-07-13 14:51:51
运行 ID: 51806
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define For(i,s,t) for (int i = (s); i <= (t); ++i) #define Rof(i,s,t) for (int i = (s); i >= (t); --i) using LL = long long; using Pii = pair<int, int>; template <typename T> using Heap = __gnu_pbds::priority_queue<T, greater<T>, __gnu_pbds::pairing_heap_tag>; template <typename T> using BST = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; const int inf = 0x3f3f3f3f; const LL infLL = 0x3f3f3f3f3f3f3f3fLL; const int maxn = 3000 + 5; const int maxs = (1 << 12) + 5; int n, ALL; int f[maxn][maxs][2]; int jump[maxn], last[12]; struct State { int i, S; }; bool vis[maxn][maxs][2]; void expand(int i, int S, vector<State> &tmp) { if (vis[i][S][0]) return; vis[i][S][0] = true; if (f[i+1][S][1] == f[i][S][0] && !vis[i+1][S][1]) { vis[i+1][S][1] = true; tmp.push_back(State{i + 1, S}); } if (f[i+1][S][0] == f[i][S][0]) expand(i + 1, S, tmp); } struct MarriageAndSelectionChallenge { string solve(string s) { n = s.length(); ALL = (1 << 12) - 1; memset(f, 0xc0, sizeof(f)); f[n][0][0] = 0; rep(c,12) last[c] = n; Rof(i,n-1,0) { int c = s[i] - 'a'; jump[i] = last[c], last[c] = i; For(S,0,ALL) { f[i][S][0] = max(f[i+1][S][0], f[i+1][S][1]); if ((S >> c) & 1) { f[i][S][1] = max(f[jump[i]][S][1], max(f[i+1][S^(1<<c)][0], f[i+1][S^(1<<c)][1])) + 1; } } } int l = 0; For(S,0,ALL) l = max(l, max(f[0][S][0], f[0][S][1])); vector<State> vec; memset(vis, false, sizeof(vis)); rep(i,n) For(S,0,ALL) if (f[i][S][1] == l) { vis[i][S][1] = true; vec.push_back(State{i, S}); } string ret; Rof(j,l,1) { char best = 'z'; for (const State &sta: vec) best = min(best, s[sta.i]); ret += best; vector<State> tmp; int c = best - 'a'; for (const State &sta: vec) { int i = sta.i, S = sta.S; if (s[i] != best) continue; if (f[jump[i]][S][1] + 1 == f[i][S][1] && !vis[jump[i]][S][1]) { vis[jump[i]][S][1] = true; tmp.push_back(State{jump[i], S}); } if (f[i+1][S^(1<<c)][0] + 1 == f[i][S][1]) { expand(i + 1, S ^ (1 << c), tmp); } if (f[i+1][S^(1<<c)][1] + 1 == f[i][S][1] && !vis[i+1][S^(1<<c)][1]) { vis[i+1][S^(1<<c)][1] = true; tmp.push_back(State{i + 1, S ^ (1 << c)}); } } vec.swap(tmp); } return ret; } }; int main() { freopen("seq.in", "r", stdin); freopen("seq.out", "w", stdout); int n, k, type; cin >> n >> k >> type; string s; cin >> s; string ret = (new MarriageAndSelectionChallenge())->solve(s); cout << ret.length() << endl; if (type) cout << ret << endl; return 0; }