本地没有对了,但交上去RE
https://www.luogu.com.cn/record/182526762
#include<bits/stdc++.h>
using namespace std;
int n,f[2000006],pre[2000006],ans=0;
char s[2000006];
int main() {
cin>>n;
cin>>s+1;
for(int i=1; i<=n; i++) {
pre[i]=i-1;
while(s[i]!=s[pre[i]]&&pre[i]>0) pre[i]=pre[pre[i]]-1;
}
for(int i=1; i<=n; i++) if(pre[i]>0) f[i]=f[pre[i]-1]+1;
for(int i=1; i<=n; i++) ans+=f[i];
cout<<ans;
return 0;
}