Lang:G++
Edit12345678910111213141516171819202122232425262728293031#include<iostream>#include<cstdio>#include<cstring>#define rg register#define rep(i,x,y) for(rg int i=(x);i<=(y);++i)#define per(i,x,y) for(rg int i=(x);i>=(y);--i)using namespace std;typedef long long LL;const int N=1e6+10,P=N<<1,alp=26;inline bool vaild(char c){return c>='a'&&c<='z';}inline char gc(){char c=getchar();while(!vaild(c))c=getchar();return c;}int n,s[N];struct SAM{int son[P][alp],pr[P],step[P],tot,last;SAM(){last=tot=1;}void ins(int c,int l){int k=++tot;step[k]=l;while(last&&son[last][c]==0)son[last][c]=k,last=pr[last];if(last){int q=son[last][c];if(step[q]==step[last]+1)pr[k]=q;else{int nq=++tot;step[nq]=step[last]+1;memcpy(son[nq],son[q],sizeof(son[q]));pr[nq]=pr[q];pr[q]=pr[k]=nq;while(last&&son[last][c]==q)son[last][c]=nq,last=pr[last];}}else pr[k]=1;last=k;