http://blog.sina.com.cn/s/blog_7812e98601012cim.html//suffix Automaton set up detailed introduction
http://www.tuicool.com/articles/Mjuu2y//Suffix Automata learning Guide (exercise list)
1 Constmaxn=10008;2 varA,f,rig:Array[0.. MAXN] ofLongint;3Nt:Array[0.. MAXN,'a'..'Z'] ofLongint;4 Last,sum,i:longint;5 s:ansistring;6 7egArray[0.. maxn*2] of RecordNt,v:longint;End;8LtArray[0.. MAXN] ofLongint;9 El:longint;Ten One procedureSam_init; A begin -Fillchar (F,sizeof (f),255); -Fillchar (Nt,sizeof (NT),255); theFillchar (A,sizeof (a),0); -last:=0; sum:=0; - End; - + procedureSam_ins (Ch:char); - varNext,p,np,q,nq:longint; + begin Anext:=Nt[last][ch]; add multiple strings to use at//if(next<>-1) and(f[last+1]<>f[next]) Then beginLast:=next; ExitEnd; -Inc (SUM); P:=last; Np:=sum; a[np]:=a[p]+1; LAST:=NP; rig[np]:=1; - while(p<>-1) and(nt[p][ch]=-1) Do beginNT[P][CH]:=NP; P:=F[P];End; - ifp=-1 Thenf[np]:=0 Else - begin -q:=Nt[p][ch]; in ifa[p]+1=A[Q] ThenF[np]:=qElse - begin toInc (SUM); Nq:=sum; a[nq]:=a[p]+1; +nt[nq]:=Nt[q]; -F[NQ]:=F[Q]; F[Q]:=NQ; f[np]:=NQ; the while(p<>-1) and(NT[P][CH]=Q) Do beginNT[P][CH]:=NQ; P:=F[P];End; * End; $ End;Panax Notoginseng End; - the procedureSam_visit; //Traverse all suffixes + varX:longint; AV:Array[0.. MAXN] ofBoolean; the proceduredfs (now:longint; t:string); + varC:char; - begin $ ifV[now] ThenWriteln (t); $ forc:='a' to 'Z' Do - ifnt[now][c]<>-1 Then -DFS (nt[now][c],t+c); the End; - beginWuyix:=Last ; the whileX<>0 Do beginV[x]:=true; X:=F[X];End; -Dfs0,"'); Wu End; - About proceduresam_visit1;//Traverse all substrings and count times $ proceduredfs (now:longint; t:string); - varC:char; - begin - ifNow<>0 ThenWriteln (T,' ', Rig[now]); A forc:='a' to 'Z' Do + ifnt[now][c]<>-1 Then theDFS (nt[now][c],t+c); - End; $ begin theDfs0,"'); the End; the the functionSam_calc:longint; //a string of essentially different - varI,cnt:longint; in begin thecnt:=0; the fori:=1 toSum Docnt:=cnt+a[i]-A[f[i]]; About exit (CNT); the End; the the procedureSam_rig; + procedureDFS (u:longint); - varI:longint; the beginBayii:=Lt[u]; the whileI<>0 Do the begin - DFS (EG[I].V); -rig[u]:=rig[u]+RIG[EG[I].V]; thei:=Eg[i].nt; the End; the End; the procedureAdd (u,v:longint); - begin the Inc. (EL); theeg[el].v:=v; theeg[el].nt:=Lt[u];94lt[u]:=el; the End; the begin theel:=0;98Fillchar (lt,sizeof (LT),0); About fori:=1 toSum DoAdd (f[i],i); -Dfs0);101 End;102 103 begin104 Sam_init; the Readln (s);106 fori:=1 toLength (s) Dosam_ins (S[i]);107 Sam_rig;108 sam_visit1;109 End.
Automatic suffix machine