100pts
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
inline void write(int x){
if(x==0){putchar('0');return;}
int len=0,k1=x,c[10005];
if(k1<0)k1=-k1,putchar('-');
while(k1)c[len++]=k1%10+'0',k1/=10;
while(len--)putchar(c[len]);
}
const int N=2e6+5;
char s[N];
struct HASH_STACK{
const int p=20070903,Inv1=674759451,Mod1=999911659,Inv2=489437524,Mod2=1e9+7;
int Calc1(int a,int b){
a+=b;
if(a>=Mod1)return a-Mod1;
else return a;
}
int Del1(int a,int b){
a-=b;
if(a<=0)return a+Mod1;
else return a;
}
int Mul1(int a,int b){return a*b%Mod1;}
int Calc2(int a,int b){
a+=b;
if(a>=Mod2)return a-Mod2;
else return a;
}
int Del2(int a,int b){
a-=b;
if(a<=0)return a+Mod2;
else return a;
}
int Mul2(int a,int b){return a*b%Mod2;}
int stop=0,Pos[N],Hash1=0,Hash2=0;
void Push(int a){
Hash1=Mul1(p,Hash1),Hash1=Calc1(Hash1,s[a]);
Hash2=Mul2(p,Hash2),Hash2=Calc2(Hash2,s[a]);
Pos[++stop]=a;
}
void Pop(){
Hash1=Del1(Hash1,s[Pos[stop]]),Hash1=Mul1(Hash1,Inv1);
Hash2=Del2(Hash2,s[Pos[stop]]),Hash2=Mul2(Hash2,Inv2);
--stop;
}
char Top(){return s[Pos[stop]];}
int Get(){return (Hash1<<32ll)+Hash2;}
}St;
unordered_map<int,int> Map;
signed main(){
int n=read(),ans=0;cin>>s+1;
Map[0]=1;
for(int i=1;i<=n;i++){
if(St.Top()==s[i])St.Pop();
else St.Push(i);
ans+=Map[St.Get()];
Map[St.Get()]++;
}
cout<<ans;
return 0;
}
25pts
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
inline void write(int x){
if(x==0){putchar('0');return;}
int len=0,k1=x,c[10005];
if(k1<0)k1=-k1,putchar('-');
while(k1)c[len++]=k1%10+'0',k1/=10;
while(len--)putchar(c[len]);
}
const int N=2e6+5;
char s[N];
struct HASH_STACK{
const int p=1e9+9,Inv1=674759451,Mod1=999911659,Inv2=489437524,Mod2=1e9+7;
int Calc1(int a,int b){
a+=b;
if(a>=Mod1)return a-Mod1;
else return a;
}
int Del1(int a,int b){
a-=b;
if(a<=0)return a+Mod1;
else return a;
}
int Mul1(int a,int b){return a*b%Mod1;}
int Calc2(int a,int b){
a+=b;
if(a>=Mod2)return a-Mod2;
else return a;
}
int Del2(int a,int b){
a-=b;
if(a<=0)return a+Mod2;
else return a;
}
int Mul2(int a,int b){return a*b%Mod2;}
int stop=0,Pos[N],Hash1=0,Hash2=0;
void Push(int a){
Hash1=Mul1(p,Hash1),Hash1=Calc1(Hash1,s[a]);
Hash2=Mul2(p,Hash2),Hash2=Calc2(Hash2,s[a]);
Pos[++stop]=a;
}
void Pop(){
Hash1=Del1(Hash1,s[Pos[stop]]),Hash1=Mul1(Hash1,Inv1);
Hash2=Del2(Hash2,s[Pos[stop]]),Hash2=Mul2(Hash2,Inv2);
--stop;
}
char Top(){return s[Pos[stop]];}
int Get(){return (Hash1<<32ll)+Hash2;}
}St;
unordered_map<int,int> Map;
signed main(){
int n=read(),ans=0;cin>>s+1;
Map[0]=1;
for(int i=1;i<=n;i++){
if(St.Top()==s[i])St.Pop();
else St.Push(i);
ans+=Map[St.Get()];
Map[St.Get()]++;
}
cout<<ans;
return 0;
}