求不同子串个数
裸的后缀自动机
1 #include<cstring> 2 #include<cmath> 3 #include<iostream> 4 #include<algorithm> 5 #include<cstdio> 6 7 #define ll long long 8 #define N 2000007 9 using namespace std; 10 inline int read() 11 { 12 int x=0,f=1;char ch=getchar(); 13 while(!isdigit(ch)){if(ch==‘-‘)f=-1;ch=getchar();} 14 while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-‘0‘;ch=getchar();} 15 return x*f; 16 } 17 18 int n; 19 ll ans; 20 struct sam 21 { 22 int last,cnt; 23 int c[N][26],fa[N],mx[N]; 24 sam(){last=cnt=1;} 25 void extend(int x) 26 { 27 int p=last,np=last=++cnt;mx[np]=mx[p]+1; 28 while(p&&!c[p][x]) 29 { 30 c[p][x]=np; 31 p=fa[p]; 32 } 33 if(!p)fa[np]=1; 34 else 35 { 36 int q=c[p][x]; 37 if(mx[q]==mx[p]+1)fa[np]=q; 38 else 39 { 40 int nq=++cnt;mx[nq]=mx[p]+1; 41 memcpy(c[nq],c[q],sizeof(c[q])); 42 fa[nq]=fa[q]; 43 fa[q]=fa[np]=nq; 44 while(c[p][x]==q)c[p][x]=nq,p=fa[p]; 45 } 46 } 47 } 48 }sam; 49 char s[N]; 50 51 int main() 52 { 53 scanf("%s",s+1);n=strlen(s+1); 54 for (int i=1;i<=n;i++) 55 sam.extend(s[i]-‘a‘); 56 for (int i=1;i<=sam.cnt;i++) 57 ans+=sam.mx[i]-sam.mx[sam.fa[i]]; 58 printf("%lld",ans); 59 }