rt
洛谷AC
求怎么死的
#include<bits/stdc++.h>
using namespace std;
int n;
string str;
int q[2000010],f[2000010];
int last[210][2000010],tot[210];
long long ans=0;
int main()
{
freopen("game.in","r",stdin);
freopen("game.out","w",stdout);
cin>>n>>str;
for(int i=0;i<n;i++)
{
int now=(int){str[i]};
q[i]=-1;
if(tot[now]==0)
{
last[now][0]=i;
tot[now]=1;
continue;
}
if(tot[now]!=0)
{
if(last[now][tot[now]-1]==i-1)
{
q[i]=i-1;
last[now][tot[now]]=i;
tot[now]++;
continue;
}
int k=q[i-1],tar=last[now][tot[now]-1],j=1;
while(j<=tot[now])
{
while(k>tar+1)
{
k=q[k-1];
}
if(k==-1)
{
q[i]=-1;
break;
}
if(k==tar+1)
{
q[i]=tar;
break;
}
else
{
j+=2;
tar=last[now][tot[now]-j];
}
}
last[now][tot[now]++]=i;
}
}
//for(int i=0;i<n;i++)cout<<q[i]<<" ";
for(int i=0;i<n;i++)
{
if(q[i]!=-1)f[i]=f[q[i]-1]+1;
//cout<<f[i]<<endl;
ans+=f[i];
}
cout<<ans<<endl;
fclose(stdin);
fclose(stdout);
return 0;
}