不存在两个相邻字串满足若不移动左右字串不可移动中间,而移动后可以移动中间。
据此,列出暴力转移:
其中 至多两种字符。
其中 的左端点可以用双指针维护。
形式化的,先考虑 ,记 从双色段开始到现在的数量前缀和,有:
这个东西容斥一下:
所以 的贡献函数,传入 ,就出来了:
如果当前并不是双色了,那直接清空李超线段树,然后重构,对于每个点最多被算两次,复杂度为 。
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <random>
#include <cstring>
#include <queue>
namespace {
typedef long long lnt;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const int max_n = 1e6 + 10, max_k = 31;
}
struct line {
lnt k, b;
lnt operator ()(int x) { return k * x + b; }
line() = default;
line(lnt _k, lnt _b) : k(_k), b(_b) {}
} f[max_n];
int cmp(int a, int b, int x) { // 返回 f[a](x) < f[b](x)
if (a == -1 || b == -1) return a == -1;
lnt va = f[a](x), vb = f[b](x);
return a == b ? (a > b) : (va < vb);
}
int maxln(int a, int b, int x) { return cmp(a, b, x) ? b : a; }
struct segt { // 0-index
inline static int lf(int x) { return x << 1; }
inline static int rt(int x) { return lf(x) | 1; }
int id[max_n << 2];
int _m_R;
void _build(int cur, int ll, int rr) {
id[cur] = -1;
if (!(ll == rr - 1)) {
int mid = (ll + rr) >> 1;
_build(lf(cur), ll, mid), _build(rt(cur), mid, rr);
}
}
void _clear(int cur, int ll, int rr) {
id[cur] = -1;
if (!(ll == rr - 1)) {
int mid = (ll + rr) >> 1;
if (id[lf(cur)] != -1) _clear(lf(cur), ll, mid);
if (id[rt(cur)] != -1) _clear(rt(cur), mid, rr);
}
}
void build(int n) {
_m_R = n, _build(1, 0, _m_R);
}
void clear() {
_clear(1, 0, _m_R);
}
void _update(int cur, int ll, int rr, int _id) {
int mid = (ll + rr) >> 1;
if (id[cur] == -1 || cmp(id[cur], _id, mid)) std::swap(id[cur], _id);
if (ll == rr - 1 || _id == -1) return;
if (cmp(id[cur], _id, ll)) _update(lf(cur), ll, mid, _id);
if (cmp(id[cur], _id, rr - 1)) _update(rt(cur), mid, rr, _id); // else 实际上只有一种
}
int _query(int cur, int ll, int rr, int b) {
if (ll == rr - 1) {
return id[cur];
} else {
int mid = (ll + rr) >> 1;
if (b < mid) return maxln(id[cur], _query(lf(cur), ll, mid, b), b);
else return maxln(id[cur], _query(rt(cur), mid, rr, b), b);
}
}
int query(int b) {
return _query(1, 0, _m_R, b);
}
void update(int _id) {
_update(1, 0, _m_R, _id);
}
} t;
int n;
lnt dp[max_n];
std::string s;
void _solve(int l, int r, int LF, int RT) {
std::vector<int> cnt(2, 0);
t.update(0);
lnt s1 = 0;
for (int i = l; i <= r; ++i) {
f[i] = line(-cnt[0], dp[i - 1] + s1);
t.update(i);
++cnt[s[i] == RT];
if (s[i] == LF) s1 += cnt[1];
lnt t_i = lnt(cnt[0]) * cnt[1] - s1;
dp[i] = std::max({dp[i], dp[i - 1], f[t.query(cnt[1])](cnt[1]) + t_i});
}
t.clear();
}
void solve() {
std::cin >> n >> s, s.insert(s.begin(), '\0'), s.insert(s.end(), 'D');
std::fill(dp, dp + n + 1, 0);
std::vector<int> cnt(4, 0);
for (int i = 1, l = 1, now_c = 0; i <= n + 1; ++i) {
now_c += (++cnt[s[i] - 'A'] == 1);
if (now_c == 3) {
--cnt[s[i] - 'A'];
int LF, RT;
if (cnt[0] && cnt[1]) LF = 'A', RT = 'B';
if (cnt[1] && cnt[2]) LF = 'B', RT = 'C';
if (cnt[2] && cnt[0]) LF = 'C', RT = 'A';
_solve(l, i - 1, LF, RT);
++cnt[s[i] - 'A'];
while (now_c != 2) now_c -= (--cnt[s[l++] - 'A'] == 0);
}
}
std::printf("%lld\n", dp[n]);
}
int main() {
t.build(max_n);
std::cin.tie(nullptr)->sync_with_stdio(false);
int T = 1; std::cin >> T;
for (int i = 0; i < T; ++i) {
solve();
}
return 0;
}