理解神马的就不贴了,实在需要的话留言,我再填坑。
#include <cstdio> #include <cstring> #include <algorithm> #define N 101000 #define T 26 using namespace std; int pa[N<<1],son[N<<1][T]; int deep[N<<1],cnt,root,last; char str[N]; inline int Newnode(int _deep){deep[++cnt]=_deep;return cnt;} inline void SAM(int alp) { int np=Newnode(deep[last]+1); int u=last; while(u&&!son[u][alp])son[u][alp]=np,u=pa[u]; if(!u)pa[np]=root; else { int v=son[u][alp]; if(deep[v]==deep[u]+1)pa[np]=v; else { int nv=Newnode(deep[u]+1); memcpy(son[nv],son[v],sizeof(son[v])); pa[nv]=pa[v],pa[v]=pa[np]=nv; while(u&&son[u][alp]==v)son[u][alp]=nv,u=pa[u]; } } last=np; } inline void pre(){root=last=Newnode(0);} int main() { freopen("test.in","r",stdin); scanf("%s",str); pre(); for(int i=0;str[i];i++)SAM(str[i]-'a'); return 0; }
原文地址:http://blog.csdn.net/vmurder/article/details/41869803