提交时间:2026-01-10 15:44:46

运行 ID: 39465

#include<bits/stdc++.h> using namespace std; #define int long long #define ull unsigned long long #define pb push_back #define pii pair<int,int> #define fr first #define sc second #define mk make_pair #define inx(u) int I=h[(u)],v=edge[I].v;I;I=edge[I].nx,v=edge[I].v const int MAXN=500010,N=30,base1=131,base2=171,Mod1=1011451423,Mod2=998244853; int read(){int x=0,f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();return x*f;} bool AAA; int n,m,ans,a[MAXN]; char s[MAXN],c[MAXN]; struct hs{ int x,y; hs operator*(const int&G)const{return {x*G%Mod1,y*G%Mod2};} hs operator+(const int&G)const{return {x+G>=Mod1?x+G-Mod1:x+G,y+G>=Mod2?y+G-Mod2:y+G};} hs operator*(const hs &G)const{return {x*G.x%Mod1,y*G.y%Mod2};} hs operator+(const hs &G)const{return {x+G.x>=Mod1?x+G.x-Mod1:x+G.x,y+G.y>=Mod2?y+G.y-Mod2:y+G.y};} hs operator-(const hs &G)const{return {x-G.x<0?x-G.x+Mod1:x-G.x,y-G.y<0?y-G.y+Mod2:y-G.y};} bool operator<(const hs&G)const{return x==G.x?y<G.y:x<G.x;} bool operator==(const hs&G)const{return x==G.x&&y==G.y;} }jc[MAXN],h[MAXN],tp[MAXN]; hs qry(int l,int r){return h[r]-h[l-1]*jc[r-l+1];} int p[N]; map<hs,int>mp,ty,ph[MAXN],ttp;int cnt; vector<int>G[MAXN]; vector<pair<hs,hs>>Q[MAXN]; struct HSH{ ull operator()(const pair<hs,hs>&G)const{return (G.fr.x+G.fr.y)<<31|(G.sc.x+G.sc.y);} }; unordered_map<pair<hs,hs>,int,HSH>MP; hs now[MAXN];int k; void slv(){ n=read(); for(int i=1;i<=n;i++){ scanf("%s",s+1); a[i]=strlen(s+1)+a[i-1]; for(int j=a[i-1]+1;j<=a[i];j++)c[j]=s[j-a[i-1]],h[j]=h[j-1]*jc[1]+((hs){c[j],c[j]}),p[c[j]-'a']++; for(int j=0;j<N;j++){ for(int o=1;o<=p[j];o++)tp[i]=tp[i]*jc[0]+(hs){j+'a',j+'a'}; p[j]=0; } if(!ty.count(tp[i]))ty[tp[i]]=++cnt; G[ty[tp[i]]].pb(i); } m=a[1]; for(int i=1;i<=cnt;i++)ans+=3333ll*G[i].size()*(n-G[i].size()); for(int e=1;e<=cnt;e++){ ans+=G[e].size()*(G[e].size()-1); if(n<=m){ for(auto o:G[e]){ int lst=a[o-1]+1; for(int i=a[o-1]+1;i<=a[o];i++)if(i==a[o]||c[i+1]<c[i]){ pair<hs,hs>tmp=mk(qry(a[o-1]+1,lst-1),qry(i+1,a[o])); for(auto r:G[e])if(r!=o){ ans-=tmp==mk(qry(a[r-1]+1,a[r-1]+lst-a[o-1]-1),qry(i-a[o-1]+a[r-1]+1,a[r])); } lst=i+1; } } } else{ MP.clear(); for(auto o:G[e]){ for(int i=0;i<=m;i++)for(int j=i+1;j<=m+1;j++)MP[mk(qry(a[o-1]+1,a[o-1]+i),qry(a[o-1]+j,a[o-1]+m))]++; } for(auto o:G[e]){ int lst=a[o-1]+1; for(int i=a[o-1]+1;i<=a[o];i++)if(i==a[o]||c[i+1]<c[i])ans-=MP[mk(qry(a[o-1]+1,lst-1),qry(i+1,a[o]))]-1,lst=i+1; } } } printf("%lld",ans); } bool BBB; signed main(){ // freopen("string.in","r",stdin);freopen("string.out","w",stdout); jc[0]={1,1};for(int i=1;i<=MAXN-5;i++)jc[i]=jc[i-1]*((hs){base1,base2}); slv(); //cerr<<clock()*1.0/CLOCKS_PER_SEC<<"s\n"<<((&AAA)-(&BBB))/1024.0/1024<<"MB\n"; return 0; }