提交时间:2022-07-13 13:27:22
运行 ID: 51713
#include <bits/stdc++.h> using namespace std; int _n, k, type; int _a[3005]; int n = 0, a[3005], w[3005]; int f[4100]; int lastmx[15][4100]; int main() { // freopen("seq4.in", "r", stdin); // freopen("seq4.out", "w", stdout); scanf("%d%d%d", &_n, &k, &type); for (int i = 1; i <= _n; i++) { char c; scanf(" %c", &c); _a[i] = c - 'a'; } for (int l = 1, i = 2; i <= _n + 1; i++) { if (_a[i] != _a[i - 1]) { a[++n] = _a[i - 1]; w[n] = i - l; l = i; } } memset(f, ~0x3f, sizeof(f)); f[1 << a[1]] = w[1]; f[0] = 0; memset(lastmx, ~0x3f, sizeof(lastmx)); lastmx[a[1]][1 << a[1]] = w[1]; for (int i = 2; i <= n; i++) { for (int j = (1 << k) - 1; j >= 0; j--) { if (j & (1 << a[i])) { f[j] = max(f[j], max(f[j ^ (1 << a[i])], lastmx[a[i]][j]) + w[i]); // f[j] = max(f[j], max(f[j ^ (1 << a[i])], 0) + w[i]); lastmx[a[i]][j] = f[j]; } // printf("%d ", f[j]); } // printf("\n"); } int ans = 0; for (int i = 0; i <= (1 << k) - 1; i++) { // cout << f[i] << " "; ans = max(ans, f[i]); } printf("%d\n", ans); return 0; }