Test instructions: The number of different substrings of a string
n<=1000
Idea: This is a thesis question
1 varA,X,Y,SA,RANK,HEIGHT,WC,WD:Array[0.. the] ofLongint;2 N,i,m,ans,v,cas:longint;3 ch:ansistring;4 5 procedureSwapvarx,y:longint);6 varT:longint;7 begin8T:=x; X:=y; y:=T;9 End;Ten One functionCMP (A,b,l:longint): boolean; A begin -Exit ((Y[a]=y[b]) and(y[a+l]=y[b+l])); - End; the - procedureGetsa (n:longint); - varI,j,p:longint; - begin + fori:=0 toN-1 Do - begin +x[i]:=A[i]; A Inc (Wc[a[i]); at End; - fori:=1 toM-1 Dowc[i]:=wc[i-1]+Wc[i]; - fori:=n-1 Downto 0 Do - begin - Dec (wc[x[i]]); -sa[wc[x[i]]]:=i; in End; -j:=1; p:=1; to whileP<n Do + begin -p:=0; the forI:=n-j toN-1 Do * begin $y[p]:=I; Inc (P);Panax Notoginseng End; - fori:=0 toN-1 Do the ifSa[i]>=j Then beginY[p]:=sa[i]-j; Inc (P);End; + fori:=0 toN-1 Dowd[i]:=X[y[i]]; A fori:=0 toM-1 Dowc[i]:=0; the fori:=0 toN-1 DoInc (Wc[wd[i]); + fori:=1 toM-1 Dowc[i]:=wc[i-1]+Wc[i]; - fori:=n-1 Downto 0 Do $ begin $ Dec (wc[wd[i]]); -sa[wc[wd[i]]]:=Y[i]; - End; the fori:=0 toN Doswap (x[i],y[i]); -p:=1; x[sa[0]]:=0;Wuyi fori:=1 toN-1 Do the ifCMP (sa[i-1],SA[I],J) Thenx[sa[i]]:=p-1 - Else beginX[sa[i]]:=p; Inc (P);End; Wuj:=j*2; -m:=p; About End; $ End; - - proceduregetheight (n:longint); - varI,j,k:longint; A begin +k:=0; the fori:=1 toN Dorank[sa[i]]:=i; - fori:=0 toN-1 Do $ begin the ifK>0 ThenDec (k); thej:=sa[rank[i]-1]; the whileA[I+K]=A[J+K] DoInc (k); theheight[rank[i]]:=K; - End; in End; the the begin AboutAssign (input,'spoj694.in'); Reset (input); theAssign (output,'Spoj694.out'); Rewrite (output); the Readln (CAS); the forv:=1 toCas Do + begin -Fillchar (A,sizeof (a),0); theFillchar (x,sizeof (x),0);BayiFillchar (y,sizeof (y),0); theFillchar (sa,sizeof (SA),0); theFillchar (rank), rank,sizeof,0); -Fillchar (wc,sizeof (WC),0); -Fillchar (Wd,sizeof (WD),0); theFillchar (height,sizeof (height),0); theREADLN (CH); n:=Length (CH); the fori:=0 toN-1 DoA[i]:=ord (ch[i+1]); thea[n]:=0; m:= -; -Getsa (n+1); the getheight (n); theans:=0; the fori:=1 toN Doans:=ans+n-sa[i]-Height[i];94 writeln (ans); the End; the the close (input);98 close (output); About End.
"spoj694&705" Distinct substrings (suffix array)