Portal
In fact, this question is to give a string, and then asked how many different sub-strings each have a prefix.
Also with \ (len \) nature of the array, if a new node is added \ (X \) , then it is for a different number of sub-strings entire string has been added is added \ (len [x] -len [ fa [x ]] \) .
I tried to explain why, because \ (fa [x] \) all represent a certain string is \ (x \) suffix, \ (x \) new string length must be greater than \ (len [fa [x ]] \) , then the \ (X \) string length is represented by \ (len [fa [x] ] + 1 ... len [x] \) for the new string is, of course, the only thing length, Therefore, the new number is \ (len [the X-] -len [FA [the X-]] \) .
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+10;
int last=1,tot=1,n,fa[N*2],len[N*2];
map<int,int> ch[N*2];
LL ans;
int newnode(int id){fa[++tot]=fa[id];len[tot]=len[id];ch[tot]=ch[id];return tot;}
void insert(int c){
int p=last,np=last=newnode(0);
len[np]=len[p]+1;
for(;p&&!ch[p][c];p=fa[p]) ch[p][c]=np;
if(!p) fa[np]=1;
else{
int q=ch[p][c];
if(len[q]==len[p]+1) fa[np]=q;
else{
int nq=newnode(q);len[nq]=len[p]+1;
fa[q]=fa[np]=nq;
for(;p&&ch[p][c]==q;p=fa[p]) ch[p][c]=nq;
}
}
ans+=len[np]-len[fa[np]];
printf("%lld\n",ans);
}
int main(){
scanf("%d",&n);
for(int i=1,x;i<=n;i++){
scanf("%d",&x);
insert(x);
}
return 0;
}