Template:
1. Quick-Line:
Var
Rank,height,h:array [0..300000] of Qword;
Rank[i]:i the suffix of the ranking;
HEIGHT[I]:LCP (S[a[i,2]...len],s[a[i-1,2]...len]);
H[I]:LCP (S[i...len],s[a[rank[i]-1,2]...len]);
h[i]>=h[i]-1;
A:array [1..300000,1..2] of Qword;
F:array [1..300000] of Boolean;
s:string;
I,j,m,n,k,mm:longint;
X:qword;
Procedure cha (l,r:longint);
Var
I,j:longint;
Mid,t:qword;
Begin
I:=l;
J:=r;
mid:=a[(l+r) div 2,1];
While I<=j do
Begin
While A[i,1]<mid do Inc (i);
While A[j,1]>mid do Dec (j);
if (I<=J) then
Begin
t:=a[i,1];
a[i,1]:=a[j,1];
a[j,1]:=t;
t:=a[i,2];
a[i,2]:=a[j,2];
a[j,2]:=t;
Inc (I);
Dec (j);
End
End
If J>l then cha (l,j);
If I<r then cha (i,r);
End
Begin
Assign (input, ' sss.in ');
Assign (output, ' sss.out ');
Reset (input);
Rewrite (output);
READLN (s);
For i:=1 to Length (s) do
Begin
A[i,1]:=ord (S[i])-ord (' a ') +1;
A[i,2]:=i;
rank[i]:=a[i,1];
End
M:=1;
While N<>length (s) do
Begin
n:=0;
If M<>0 Then
For i:=1 to Length (s) do
Begin
x:=rank[i]*1000000;
X:=X+RANK[I+M];
A[i,1]:=x;
A[i,2]:=i;
End
Cha (1,length (s));
N:=1;
Rank[a[1,2]]:=1;
For i:=2 to Length (s) do
Begin
If a[i,1]<>a[i-1,1] Then
Inc (N);
Rank[a[i,2]]:=n;
End
m:=m*2;
End
For i:=1 to Length (s) do
Write (a[i,2], ");
Writeln;
j:=0;
For i:=1 to Length (s) do
Begin
If J>0 then Dec (j);
while (rank[i]-1<>0) and (S[i+j]=s[a[rank[i]-1,2]+j]) do Inc (J);
H[i]:=j;
Height[rank[i]]:=j;
End
For i:=1 to Length (s) do
Write (Height[i], ");
Writeln;
For i:=1 to Length (s) do
Write (H[i], ");
Writeln;
Close (input);
Close (output);
End.
2. Bucket row
Var
Rank,sa,lis,a:array [1..10000] of Longint;
F:array [1..10000] of Boolean;
s:string;
I,j,m,n,k:longint;
Begin
READLN (s);
M:=1;
For i:=1 to Length (s) do
Rank[i]:=ord (S[i])-ord (' a ') +1;
While N<>length (s) do
Begin
n:=0;
For i:=1 to Length (s) do
A[I]:=RANK[I]*100+RANK[I+M];
Fillchar (F,sizeof (f), false);
For i:=1 to Length (s) do
Begin
If not f[a[i] "then
Begin
Inc (N);
F[a[i]]:=true;
End
End
k:=0;
For I:=1 to 2626 do
If F[i] Then
Begin
Inc (k);
Lis[i]:=k;
End
For i:=1 to Length (s) do
Rank[i]:=lis[a[i]];
m:=m*2;
End
For i:=1 to Length (s) do
Write (Rank[i], ");
Writeln;
End.
The multiplication algorithm explains :
Example 1:
Sa:1 2 3 5 1 1 2 4
First time sort rank:102 203 305 501 101 102 204 400
Sa:2 3 5 7 1 2 4 6
Second Sort rank:205 307 501 702 104 206Sa:2 4 6 8 1 4 5 7
Example 2:sa:1 1 2 1 1 1 1 2first time sort rank:101 102 201 101 101 101 102sa:1 2 4 1 1 1 2 3Second sort rank:104 201 401 101 102 103Sa:4 6 8 1 2 3 5 7
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
X. Array of suffixes