1 solutions
-
0
/* * __----~~~~~~~~~~~------___ * . . ~~//====...... __--~ ~~ * -. \_|// |||\\ ~~~~~~::::... /~ * ___-==_ _-~o~ \/ ||| \\ _/~~- * __---~~~.==~||\=_ -_--~/_-~|- |\\ \\ _/~ * _-~~ .=~ | \\-_ '-~7 /- / || \ / * .~ .~ | \\ -_ / /- / || \ / * / ____ / | \\ ~-_/ /|- _/ .|| \ / * |~~ ~~|--~~~~--_ \ ~==-/ | \~--===~~ .\ * ' ~-| /| |-~\~~ __--~~ * |-~~-_/ | | ~\_ _-~ /\ * / \ \__ \/~ \__ * _--~ _/ | .-~~____--~-/ ~~==. * ((->/~ '.|||' -_| ~~-/ , . _|| * -_ ~\ ~~---l__i__i__i--~~_/ * _-~-__ ~) \--______________--~~ * //.-~~~-~_--~- |-------~~~~~~~~ * //.-~~~--\ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * 神兽保佑 永无BUG */ /* * @Description: I want to be the weakest vegetable in the world! * @Author: I.Z. */ #include<bits/stdc++.h> using namespace std; #define MOD 998244353 #define mod 993244853 #define int long long #define pii pair<int,int> #define all(v) v.begin(),v.end() #define pb push_back #define REP(i,b,e) for(int i=(b);i<(int)(e);++i) #define over(x) {cout<<(x)<<endl;return;} #define lowbit(x) ((x)&(-(x))) #define cntbit(x) __builtin_popcount(x) #define rf(v) shuffle(all(v),sd); #define deal(v) sort(all(v));v.erase(unique(v.begin(),v.end()),v.end()) #define lbound(v,x) lower_bound(all(v),x)-v.begin() mt19937 sd(random_device{}()); int qpow(int a,int b,int m=MOD,int res=1){ a%=m; while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1; return res; } string YN(bool x,string Y="Yes",string N="No") { if(x){ return Y; } else { return N; } } int exgcd(int x,int y,int &a,int &b){ if(y==0){ a=1;b=0; return x; } int d=exgcd(y,x%y,a,b); int z=b; b=a-b*(x/y); a=z; return d; } int _x_,_y_; inline int inverse(int x,int y=MOD){ exgcd(x,y,_x_,_y_); return (_x_+y)%y; } int fac[2000005],inv[2000005]; void init(int n){ fac[0]=inv[0]=1; REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD; inv[n]=qpow(fac[n],MOD-2); for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD; } int binom(int x,int y){ return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD; } string S; int sa[300005],rk[300005],ht[300005],Id[300005],old[300005],st[300005][20],cnt[300005]; bool same(int x,int y,int w,int n){return x+w<n&&y+w<n&&old[x]==old[y]&&old[x+w]==old[y+w];} int query(int x,int y){ if(rk[x]>rk[y])swap(x,y); x=rk[x]+1,y=rk[y]; int s=__lg(y-x+1); return min(st[x][s],st[y-(1<<s)+1][s]); } bool cmp(int x,int y,int X,int Y){ int l=query(x,X); // cout<<x<<' '<<y<<' '<<X<<' '<<Y<<' '<<l<<' '<<(min(y,Y)==l? (y>Y):(S[x+y]>S[X+Y]))<<endl; return min(y,Y)==l? (y>Y):(S[x+l]>S[X+l]); } void buildsa(){ int n=S.size(),m=200; REP(i,0,m)cnt[i]=0; REP(i,0,n)++cnt[rk[i]=S[i]]; REP(i,1,m)cnt[i]+=cnt[i-1]; REP(i,0,n)sa[--cnt[rk[i]]]=i; for(int w=1;w<n;w<<=1){ int cur=0; for(int i=n-1;i>=n-w;--i)Id[cur++]=i; REP(i,0,n)if(sa[i]>=w)Id[cur++]=sa[i]-w; REP(i,0,m)cnt[i]=0; REP(i,0,n)++cnt[rk[Id[i]]],old[i]=rk[i]; REP(i,1,m)cnt[i]+=cnt[i-1]; for(int i=n-1;i>=0;--i)sa[--cnt[rk[Id[i]]]]=Id[i]; cur=1;rk[sa[0]]=0; REP(i,1,n)if(same(sa[i],sa[i-1],w,n))rk[sa[i]]=cur-1;else rk[sa[i]]=cur++; if(cur==n)break;else m=cur; } int k=0; REP(i,0,n)if(rk[i]){ int t=sa[rk[i]-1];k=max(0ll,k-1); while(i+k<n&&t+k<n&&S[i+k]==S[t+k])++k; ht[rk[i]]=k; } REP(i,1,n)st[i][0]=ht[i]; REP(j,0,__lg(n-1)){ REP(i,1,n-(1<<(j+1))+1)st[i][j+1]=min(st[i][j],st[i+(1<<j)][j]); } REP(i,0,n)cnt[i]=0; } uniform_int_distribution<int>rd(MOD/2,mod-1),rd2(mod/2,mod-1); int h1[300005],h2[300005],h3[200],h4[200]; int gethash(string s){ int r1=0,r2=0; REP(i,0,s.size())r1+=h1[i]*h3[(int)s[i]]%MOD,r2+=h2[i]*h4[(int)s[i]]%mod; r1%=MOD;r2%=mod; return (r1<<32)|r2; } int n; string s[300005],s2[300005]; int cnt2[300005],ct[300005][26]; int son[300005][26],fa[300005],stt[300005],id[300005]; int tot; vector<int>nx[300005],pv[300005]; int newnode(int f=-1){ int p=tot++;id[p]=-1;fa[p]=f; REP(i,0,26)son[p][i]=-1; return p; } int cur; void dfs(int x){ if(id[x]!=-1)s2[cur]=s[id[x]],cnt2[cur]=cnt[id[x]],id[x]=cur++; REP(i,0,26)if(son[x][i]!=-1)dfs(son[x][i]); } void Main() { REP(i,0,300001)h1[i]=rd(sd),h2[i]=rd2(sd); REP(i,0,200)h3[i]=rd(sd),h4[i]=rd2(sd); map<int,int>mp; cin>>n; int ans=0;tot=0; REP(i,0,n){ string t; cin>>t; int x=gethash(t); if(mp.find(x)!=mp.end())++cnt[mp[x]]; else mp[x]=tot,cnt[tot]=1,s[tot++]=t; } n=tot;tot=0; REP(i,0,n)ans+=cnt[i]*(cnt[i]-1)*(cnt[i]-2)/6; REP(i,0,n)mp[gethash(s[i])]=cnt[i]; newnode(); REP(i,0,n){ int c=0; for(auto j:s[i]){ int k=j^96;--k; if(son[c][k]==-1)son[c][k]=newnode(c); c=son[c][k]; } id[c]=i; } dfs(0); REP(i,0,n)s[i]=s2[i],cnt[i]=cnt2[i]; REP(i,0,n)stt[i]=S.size(),S+=s[i]; buildsa(); // cout<<S<<endl; // REP(i,0,S.size())cout<<rk[i]<<' '; // cout<<endl; // REP(i,0,S.size())cout<<ht[i]<<' '; // cout<<endl; REP(i,0,n)cnt[i]=cnt2[i]; // REP(i,0,n)cout<<s[i]<<' '<<cnt[i]<<endl; int sum=0; REP(i,0,n)ans+=cnt[i]*(cnt[i]-1)/2*sum,sum+=cnt[i]; // cout<<ans<<endl; REP(i,0,tot)if(id[i]!=-1){ int d=fa[i]; while(d!=-1){if(id[d]!=-1)nx[id[d]].pb(i),pv[id[i]].pb(id[d]);d=fa[d];} } REP(i,0,tot)REP(j,0,26)ct[i][j]=0; REP(i,0,n){ for(auto j:nx[i]){ string t=""; REP(l,s[i].size(),s[id[j]].size())t+=s[id[j]][l]; if(s[i]>t)ans-=cnt[i]*cnt[i]*cnt[id[j]]-cnt[i]*(cnt[i]-1)/2*cnt[id[j]]; } } // cout<<ans<<endl; REP(i,0,n){ for(auto j:pv[i]){ // cout<<' '<<i<<' '<<j<<' '<<ans<<endl; string t=""; REP(l,s[j].size(),s[i].size())t+=s[i][l]; // cout<<t<<endl; int c=0; for(auto _:t){ int L=_-'a'; if(L!=25)ans+=ct[c][L+1]*cnt[i]*cnt[j]; if(son[c][L]==-1){c=-1;break;} c=son[c][L]; } if(c!=-1)ans+=ct[c][0]*cnt[i]*cnt[j]; } int c=0; for(auto _:s[i]){ int l=_-'a'; REP(j,0,l+1)ct[c][j]+=cnt[i]; c=son[c][l]; } // cout<<i<<' '<<ans<<endl; } REP(i,0,n){ for(auto j:pv[i]){ for(auto k:pv[i])if(j<k){ if(cmp(stt[k],s[k].size(),stt[i]+s[j].size(),s[i].size()-s[j].size())){ if(cmp(stt[j],s[j].size(),stt[i]+s[k].size(),s[i].size()-s[k].size())){ ans-=cnt[i]*cnt[j]*cnt[k];//cout<<i<<' '<<j<<' '<<k<<endl; } } } } } over(ans) } void TC() { int tc=1; while(tc--){ Main(); cout.flush(); } } signed main() { freopen("string.in","r",stdin); freopen("string.out","w",stdout); return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0; } /* 1. CLEAR the arrays (ESPECIALLY multitests) 2. DELETE useless output */
- 1
Information
- ID
- 2944
- Time
- 12000ms
- Memory
- 1024MiB
- Difficulty
- 10
- Tags
- (None)
- # Submissions
- 3
- Accepted
- 2
- Uploaded By