Look at the longest common prefix of the string, and use the suffix array + monotone stack to engage. Be sure to pay attention to long long.
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <queue>7#include <algorithm>8#include <vector>9 #defineM 2000009Ten #defineEPS 1e-10 One #defineMO 19650827 A #definell Long Long - using namespacestd; - ll Read () the { - CharCh=GetChar (); -ll x=0, f=1; - for(;ch<'0'|| Ch>'9'; ch=GetChar ()) + if(ch=='-') -f=-1; + for(; ch>='0'&&ch<='9'; ch=GetChar ()) Ax=x*Ten+ch-'0'; at returnx*F; - } - CharCh[m]; - intk,sa[2][m],rk[2][m],v[m],a[m],p=1, q,n,h[m],t; - ll Ans,b[m],c[m]; - voidWorkintSa[m],intRk[m],intSa[m],intRk[m]) in { - for(intI=1; i<=n;i++) tov[rk[sa[i]]]=i; + for(inti=n;i;i--) - if(sa[i]>K) thesa[v[rk[sa[i]-k]]--]=sa[i]-K; * for(inti=n-k+1; i<=n;i++) $sa[v[rk[i]]--]=i;Panax Notoginseng for(intI=1; i<=n;i++) -rk[sa[i]]=rk[sa[i-1]]+ (rk[sa[i]]!=rk[sa[i-1]]|| rk[sa[i]+k]!=rk[sa[i-1]+K]); the } + intMain () A { thescanf"%s", ch+1); +N=strlen (ch+1); - for(intI=1; i<=n;i++) $a[i]=ch[i]-'a'+1; $ for(intI=1; i<=n;i++) -v[a[i]]++; - for(intI=1; i<= -; i++) thev[i]+=v[i-1]; - for(intI=1; i<=n;i++)Wuyisa[p][v[a[i]]--]=i; the for(intI=1; i<=n;i++) -rk[p][sa[p][i]]=rk[p][sa[p][i-1]]+ (a[sa[p][i-1]]!=A[sa[p][i]]); Wuk=1; - for(; k<N;) About { $ Work (Sa[p],rk[p],sa[q],rk[q]); - swap (P,Q); -k<<=1; - } Ak=0; + for(intI=1; i<=n;i++) the if(rk[p][i]==1) -h[i]=0; $ Else the { the intj=sa[p][rk[p][i]-1]; the for(; a[i+k]==a[j+k]; k++); theh[i]=K; - if(K) ink--; the } thell su=0; About for(inti=n-1; i;i--) the { thell a1=h[sa[p][i+1]],s=1; the for(;b[t]>A1;) + { -s+=C[t]; thesu-=b[t]*C[t];Bayit--; the } theSu+=a1*s; - if(b[t]==A1) -c[t]+=s; the Else the { theb[++t]=A1; thed[s]=s; - } theans+=su; the } theprintf"%lld\n", (ll) (n1) *n* (n+1)/2-2*ans);94 return 0; the } the
Bzoj 3238: [Ahoi2013] Differences