模板题:UOJ35
终于可以不看板子写出后缀数组了开心【捂脸】
但还是贴一下板子吧。
要记得开双倍数组qvq
#include
#define N 200010
#define rep(i,l,r) for (int i=l;i<=r;i++)
#define per(i,r,l) for (int i=r;i>=l;i--)
using namespace std;
int n,wa[N],wb[N],cnt[N],sa[N],r[N],h[N];
char s[N];
template <class Aqua>
inline void read(Aqua &s){
s=0; Aqua f=1; char c=getchar();
while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();}
while (isdigit(c)) s=s*10+c-'0',c=getchar();
s*=f;
}
inline bool cmp(int *y,int a,int b,int len){
return (y[a]==y[b] && y[a+len]==y[b+len]);
}
void da(int m){
int j,k,p(0),*x=wa,*y=wb;
rep(i,0,m) cnt[i]=0;
rep(i,1,n)
cnt[x[i]=s[i]-96]++;
rep(i,1,m)
cnt[i]+=cnt[i-1];
per(i,n,1)
sa[cnt[x[i]]--]=i;
for (j=p=1;p1,m=p+1){
k=0;
rep(i,n-j+1,n)
y[++k]=i;
rep(i,1,n)
if (sa[i]>j)
y[++k]=sa[i]-j;
rep(i,0,m) cnt[i]=0;
rep(i,1,n)
cnt[x[y[i]]]++;
rep(i,1,m)
cnt[i]+=cnt[i-1];
per(i,n,1)
sa[cnt[x[y[i]]]--]=y[i];
for (swap(x,y),k=2,x[sa[p=1]]=1;k<=n;k++)
x[sa[k]]=cmp(y,sa[k-1],sa[k],j)?p:++p;
}
rep(i,1,n)
printf("%d ",sa[i]);
puts("");
}
void getheight(){
int ht=0;
rep(i,1,n)
r[sa[i]]=i;
rep(i,1,n){
if (ht) ht--;
if (r[i]==1) continue;
while (s[i+ht]==s[sa[r[i]-1]+ht]) ht++;
h[r[i]]=ht;
}
rep(i,2,n)
printf("%d ",h[i]);
puts("");
}
int main(){
scanf("%s",s+1);
n=strlen(s+1);
da(27);
getheight();
return 0;
}