# 后缀数组的倍增算法

var n,m,ans,st,en,i:longint; s:string; sa,rk,tsa,trk,h,sum:array[1..10000] of longint; procedure suffix; var i,j,p:longint; begin m:=255; for i:=1 to n do begin trk[i]:=ord(s[i]); inc(sum[trk[i]]); end; for i:=2 to m do inc(sum[i],sum[i-1]); for i:=n downto 1 do begin sa[sum[trk[i]]]:=i; dec(sum[trk[i]]); end; rk[sa[1]]:=1; p:=1; for i:=2 to n do begin if trk[sa[i]]<>trk[sa[i-1]] then inc(p); rk[sa[i]]:=p; end; m:=p; j:=1; while m<n do begin move(rk,trk,sizeof(rk)); fillchar(sum,sizeof(sum),0); p:=0; for i:=n-j+1 to n do begin inc(p); tsa[p]:=i; end; for i:=1 to n do if sa[i]>j then

begin inc(p); tsa[p]:=sa[i]-j; end; for i:=1 to n do begin rk[i]:=trk[tsa[i]]; inc(sum[rk[i]]); end; for i:=2 to m do inc(sum[i],sum[i-1]); for i:=n downto 1 do begin sa[sum[rk[i]]]:=tsa[i]; dec(sum[rk[i]]); end; rk[sa[1]]:=1; p:=1; for i:=2 to n do begin if (trk[sa[i]]<>trk[sa[i-1]])or(trk[sa[i]+j]<>trk[sa[i-1]+j]) then inc(p); rk[sa[i]]:=p; end; m:=p; j:=j*2; end; h[1]:=0; p:=0; for i:=1 to n do begin if rk[i]=1 then continue; j:=sa[rk[i]-1]; while s[i+p]=s[j+p] do inc(p); h[rk[i]]:=p; if p>0 then dec(p); end; for i:=1 to n do begin for j:=sa[i] to n do write(s[j]); writeln; end;

end; begin readln(s); n:=length(s); suffix; for i:=1 to n do write(sa[i],' '); writeln; for i:=1 to n do write(h[i],' '); writeln; end.

### 后缀数组

Height 数组存储相邻两个 Sa 后缀之间公共前缀的长度,如图 处理后缀树组有两种算法:倍增、DC3 (我只会倍增,所以只写倍增。。。) 首先得搞懂基数排序:基数排序-...

### 后缀数组

【关键字】 字符串,后缀,后缀数组,名次数组,基数排序, 【正文】 一、后缀数组的实现 本节主要介绍后缀数组的两种实现方法:倍增算法(Doubling Algorithm)和 DC3 ...

### 后缀数组笔记

OI 笔记]后缀数组学习笔记--后缀数组解题方法总结 2010-04-15 07:37 后缀数组...而 字符串中的其他字符都应该是大于 0 的(前面有提到,使用倍增算法前需要确保...