Describes our definition of a string with the suffix suffix (i) representing the substring from s[i] to S[length (s)].
The suffix array (Suffix array) Sa[i] holds an arrangement that satisfies Suffix (Sa[i]) <suffix (sa[i+1]) compared in dictionary order
Defines Height[i] represents the longest common prefix length between suffix (sa[i]) and suffix (sa[i-1]), where height[1]=0
Your task is to find the two arrays of SA and height. String length <=200000 input format one line, for the string in the description (only lowercase letters) output format a total of two lines, the number of n per row, the first behavior Sa[i], the second behavior Height[i], where the number of each line is separated by a space test sample 1 input
Aabaaaab
Output
4 5 6 1 7 2 8 3
0 3 2 3 1 2 0 1
Suffix array template problem just put on a very ugly template and you'll have to memorize it later. Qaq
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5 using namespacestd;6 Const intn=200050;7 intSa[n],wu[n],wv[n],wa[n],wb[n],r[n],rank[n],height[n],rank[n];8 CharS[n];9 intcmpint(RNintAintBintL) {returnR[A]==R[B] && r[a+l]==r[b+l];}TenInlinevoidSA (int(RNint*sa,intNintm) { One int*x=wa,*y=WB; A for(intI=0; i<m;++i) wu[i]=0; - for(intI=0; i<n;++i) ++wu[x[i]=R[i]]; - for(intI=1; i<m;++i) wu[i]+=wu[i-1]; the for(inti=n-1; i>=0;-I.) sa[--wu[x[i]]]=i; - for(intj=1, p=0;p <n;j<<=1, m=p) { -p=0; - for(intI=n-j;i<n;++i) y[p++]=i; + for(intI=0; i<n;++i)if(SA[I]>=J) y[p++]=sa[i]-J; - for(intI=0; i<m;++i) wu[i]=0; + for(intI=0; i<n;++i) ++wu[wv[i]=X[y[i]]; A for(intI=0; i<m;++i) wu[i]+=wu[i-1]; at for(inti=n-1; i>=0;-I.) sa[--wu[wv[i]]]=Y[i]; - swap (x, y); -p=1; -x[sa[0]]=0; - for(intI=1; i<n;++i) x[sa[i]]= (y[sa[i]]==y[sa[i-1]]) && y[sa[i]+j]==y[sa[i-1]+J])? P1: p++; - } in } -InlinevoidGet_height (int*r,intN) { to for(intI=1; i<=n;++i) rank[sa[i]]=i; + for(intI=0, j=0, k=0;i<n; height[rank[i++]]=k) - for(k?--K:0, j=sa[rank[i]-1];r[i+k]==r[j+k];++k); the } *InlinevoidOutputintx) { $ if(!x)return; Output (x/Ten);Panax NotoginsengPutchar ((%Ten)+'0'); - } the intMain () { +scanf"%s", s); A intlen=strlen (s); the for(intI=0; i<len;++i) r[i]=s[i]-'a'+1; +r[len]=0; -SA (r,sa,len+1, -); $ get_height (R,len); $ for(intI=1; i<=len;++i) Output (sa[i]+1), Putchar (' ');p UTS (""); - for(intI=1; i<=len;++i) { - if(! Height[i]) Putchar ('0'); the ElseOutput (Height[i]);p Utchar (' '); -}puts ("");Wuyi return 0; the}
tyvj1860
"tyvj1860" suffix array