boolcheck() { int sum1 = 0, sum2 = 0; int maxx1 = 0, maxx2 = 0;
for (int i = 0; i<26; i++) { sum1 += num[0][i]; maxx1 = max(maxx1, num[0][i]); }
int sur = k - (sum1 - maxx1);
for (int i = 0; i<26; i++) { sum2 += num[1][i]; maxx2 = max(maxx2, num[1][i]); } sur = sur - (sum2 - maxx2);
if (sur<0)returnfalse; elsereturntrue; }
voidsolve() { sa(k); scanf("%s", val + 1); int len = strlen(val + 1);
int en = 0; int res = 1; int flag = 0; for (int i = 1; i <= len; i++) { int flag = 0; while (true) { if (!check()) { flag = 1; break; } if (en >= len) { flag = 2; break; } num[(en+1) & 1][val[en+1] - 'a']++; en++; } if (flag == 1) res = max(res, en - i); else res = max(res, en - i + 1); num[i&1][val[i] - 'a']--; } printf("%d\n", res); }
int n; int vis[maxn]; int ans[maxn]; vector<int>edge[maxn];
structHei { int h; int num; Hei() {}; Hei(int x, int y) { h = x; num = y; } voidmerge(const Hei &b) { if (b.h>h) { h = b.h; num = b.num; } elseif (b.h == h) { num = 0; } } Hei Go_up() { returnHei(h + 1, num + 1); } };
Hei up[maxn], down[maxn];
voidcal(int x) { vis[x] = 1; down[x] = Hei(0, 0); for (int i = 0; i<edge[x].size(); i++) { int nxt = edge[x][i]; if (vis[nxt])continue; cal(nxt); down[x].merge(down[nxt].Go_up()); } vis[x] = 0; }
voidcalDown(int x) { Hei suf(0, 0); vis[x] = 1; for (int i = edge[x].size() - 1; i >= 0; i--) { int nxt = edge[x][i]; if (vis[nxt])continue; up[nxt] = suf; suf.merge(down[nxt].Go_up()); } Hei pre(0, 0); for (int i = 0; i<edge[x].size(); i++) { int nxt = edge[x][i]; if (vis[nxt])continue; Hei h = up[x]; h.merge(pre); h.merge(up[nxt]); up[nxt] = h.Go_up(); pre.merge(down[nxt].Go_up()); calDown(nxt); } pre.merge(up[x]); ans[x] = pre.num; vis[x] = 0; }