pos[][]:其实相当于当前枚举的区间的左端点的位置。(欢迎指正)
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int s[27],minn[27][27],last[27],n;
char a[1000000+5];
int ans=0,pos[27][27];
int main()
{
scanf("%d",&n);
scanf("%s",&a);
for(int i=0;i<n;i++)
{
int tmp=a[i]-96;
//cout<<tmp<<' ';
s[tmp]++;
last[tmp]=i;
for(int j=1;j<=26;j++)
{
if(tmp!=j&&s[j])
ans=max(ans,max(s[j]-s[tmp]-minn[j][tmp]-(last[tmp]==pos[j][tmp]),
s[tmp]-s[j]-minn[tmp][j]-(last[j]==pos[tmp][j])));
}
for(int j=1;j<=26;j++)
{
if(s[j]-s[tmp]<minn[j][tmp])
{
minn[j][tmp]=s[j]-s[tmp];
pos[j][tmp]=i;
}
if(s[tmp]-s[j]<minn[tmp][j])
{
minn[tmp][j]=s[tmp]-s[j];
pos[tmp][j]=i;
}
}
}
printf("%d\n",ans);
}