O(n262)过了。
#include <bits/stdc++.h>
void Freopen() {
freopen("", "r", stdin);
freopen("", "w", stdout);
}
using namespace std;
const int N = 1e6 + 10, M = 2e5 + 10, inf = 1e9, mod = 998244353;
int n;
char s[N];
int sum[26][N], lst[26];
signed main() {
ios :: sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> (s + 1);
for ( int i = 1; i <= n; i ++) {
lst[s[i] - 'a'] = i;
for ( int j = 0; j < 26; j ++) {
if (j == s[i] - 'a') sum[j][i] = sum[j][i - 1] + 1;
else sum[j][i] = sum[j][i - 1];
}
}
int ans = 0;
for ( register int a = 0; a < 26; a ++)
for ( register int b = 0; b < 26; b ++) {
if (a == b) continue ;
int mx = 0, id = 0;
for ( register int i = 1; i <= n; i ++) {
if (sum[b][i] - sum[b][id] != 0)
ans = max(ans, sum[a][i] - sum[b][i] + mx);
if (mx < sum[b][i] - sum[a][i] && i != lst[b])
mx = sum[b][i] - sum[a][i], id = i;
}
}
cout << ans;
return 0;
}