suffix (i): suffix starting from the i-th character
sa [i]: suffix array, "Who was the first few rows", i.e. suffix (sa [i]) <suffix (sa [i + 1]), 1 <= i <n.
ra [i]: ranking array, "you first few rows", ra [i] represents the suffix (i) in all suffixes in ascending order of the "rank"
height [i]: suffix (sa [i-1]) and the suffix (sa [i]) of the longest common prefix, which is the longest common prefix rank adjacent two suffixes.
For j and k, provided rank [j] <rank [j], then:
suffix (j) and the suffix (k) is the longest common prefix height [ra [j] +1], height [ra [j] +2], height [ra [j] +3], ..., height [rank [k]] is the minimum.
1, Luo Valley P3809 [template] suffix sorting
Reference herein , are used after seeking doubling method suffix array bars.
1 #include<iostream> 2 #include<sstream> 3 #include<fstream> 4 #include<algorithm> 5 #include<cstring> 6 #include<iomanip> 7 #include<cstdlib> 8 #include<cctype> 9 #include<vector> 10 #include<string> 11 #include<cmath> 12 #include<ctime> 13 #include<stack> 14 #include<queue> 15 #include<map> 16 #include<set> 17 #define mem(a,b) memset(a,b,sizeof(a)) 18 #define random(a,b) (rand()%(b-a+1)+a) 19 #define ll long long 20 #define ull unsigned long long 21 #define e 2.71828182 22 #define Pi acos(-1.0) 23 #define ls(rt) (rt<<1) 24 #define rs(rt) (rt<<1|1) 25 #define lowbit(x) (x&(-x)) 26 using namespace std; 27 const int MAXN=1e6+5; 28 char str[MAXN]; 29 int n,m; 30 int sa[MAXN],ra[MAXN],height[MAXN]; 31 int wa[MAXN],wb[MAXN],wc[MAXN],wd[MAXN]; 32 int read() 33 { 34 int s=1,x=0; 35 char ch=getchar(); 36 while(!isdigit(ch)) {if(ch=='-') s=-1;ch=getchar();} 37 while(isdigit(ch)) {x=10*x+ch-'0';ch=getchar();} 38 return x*s; 39 } 40 void GetSaRa(int n,int m)//倍增法 nlogn 41 { 42 int i,j,p,*x=wa,*y=wb,*t; 43 for(int i=0;i<m;++i) wd[i]=0; 44 for(int i=0;i<n;++i) wd[x[i]=str[i]]++; 45 for(int i=1;i<m;++i) wd[i]+=wd[i-1]; 46 for(int i=n-1;i>=0;--i) sa[--wd[x[i]]]=i; 47 for(j=1,p=1;p<n;j*=2,m=p) 48 { 49 for(p=0,i=n-j;i<n;++i) y[p++]=i; 50 for(i=0;i<n;++i) if(sa[i]>=j) y[p++]=sa[i]-j; 51 for(i=0;i<n;++i) wc[i]=x[y[i]]; 52 for(i=0;i<m;++i) wd[i]=0; 53 for(i=0;i<n;++i) wd[wc[i]]++; 54 for(i=1;i<m;++i) wd[i]+=wd[i-1]; 55 for(i=n-1;i>=0;--i) sa[--wd[wc[i]]]=y[i]; 56 for(swap(x,y),p=1,x[sa[0]]=0,i=1;i<n;++i) 57 x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+j]==y[sa[i]+j]?p-1:p++; 58 } 59 } 60 void GetHeight(int n) 61 { 62 int i,j,k=0; 63 for(i=1;i<=n;++i) ra[sa[i]]=i; 64 for(i=0;i<n;height[ra[i++]]=k) 65 for(k?k--:0,j=sa[ra[i]-1];str[i+k]==str[j+k];++k); 66 } 67 int main() 68 { 69 scanf("%s",str); 70 n=strlen(str); 71 str[n]='0';m=200; 72 GetSaRa(n+1,m); 73 //GetHeight(n); 74 //for(int i=0;i<=n;++i) cout<<sa[i]<<' ';cout<<endl; //含末尾加的字符 75 for(int i=1;i<=n;++i) cout<<sa[i]+1<<' ';cout<<endl;//不含末尾加的字符 76 //for(int i=0;i<n;++i) cout<<ra[i]<<' ';cout<<endl; 77 //for(int i=2;i<=n;++i)cout<<height[i]<<' ';cout<<endl; 78 }