struct SAM{
int now,tot,rt,last,son[500010][26],fa[500010],mx[500010],l1[500010],l2[500010],siz[500010];
void init(){
memset(son,0,sizeof(son));
tot=rt=last=1;
mx[1]=0;
}
void extend(int ch){
int p=last,np=++tot;
mx[np]=mx[p]+1;
for(;p&&!son[p][ch];p=fa[p])son[p][ch]=np;
if(!p)fa[np]=rt;
else{
int q=son[p][ch];
if(mx[q]==mx[p]+1)fa[np]=q;
else{
int nq=++tot;
mx[nq]=mx[p]+1;
memcpy(son[nq],son[q],sizeof(son[q]));
fa[nq]=fa[q];
fa[q]=fa[np]=nq;
for(;son[p][ch]==q;p=fa[p])son[p][ch]=nq;
}
}
last=np;
siz[np]=1;
}
}sam;