That's a long-learned suffix array.
Thought is still easy to understand
The biggest problem is
Code can not understand
And then, in the process of constantly simulating research,
Finally figured out a little
Just decided to write it down.
Or you'll forget qaq.
Here is the code
PS: First of all to understand the Cardinal sort
definition:c[] Array: base-ordered buckets
X[] Array: Similar to rank array, holds the rank of the string that is currently sorted at the beginning of each position
Sa[] Array: sa[i] Save the first position of the string beginning of the rank I
Y[] Array: Second keyword sort, y[i] save the first position of the string beginning of the rank I
1 voidBUILDSA (intm) {2 int*x = t, *y = t2;//This is the sort of single character3 for(inti =0; I < m; i++) C[i] =0;//emptying the C array4 for(inti =0; I < n; i++) C[x[i] = s[i]]++;//initial length is 1 o'clock, x[i] = = S[i]5 for(inti =1; I < m; i++) C[i] + = c[i-1];//Get each location6 for(inti = n-1; I >=0; i--) Sa[--c[x[i]] = i;//ranked C[x[i]], the character is from the I position, each record ranked 1, to the previous position, to ensure that the ranking and the corresponding position is not duplicated7 for(intK =1; K <= N; K <<=1) {8 intp =0;//For counting purposes.9 for(inti = n-k; I < n; i++) y[p++] = i;//0 is the smallest, put the back need to fill 0 in front of the firstTen for(inti =0; I < n; i++)if(Sa[i] >= k) y[p++] = sa[i]-K;//Sa[i] < K is not the second keyword, sa[i] 's ranking is incremented sequentially, so read directly into the Y array, minus the part that is not the second keyword before One for(inti =0; I < m; i++) C[i] =0;//emptying the C array A for(inti =0; I < n; i++) c[x[y[i]]]++;//At this point in Y has been sorted by the second key word, X is the first key word, and then directly the base of the first keyword can be sorted - for(inti =1; I < m; i++) C[i] + = c[i-1]; - for(inti = n-1; I >=0; i--) sa[--c[x[y[i] []] =Y[i]; the swap (x, y); - //discretization Ranking -p =1; x[sa[0]] =0; - for(inti =1; I < n; i++) +X[sa[i]] = y[sa[i-1] [= Y[sa[i]] && y[sa[i-1] + K] = = Y[sa[i] + K]? P-1: p++;//Two keywords are the same and ranked the same - if(P >= N) Break;//P represents the number of different rankings, if the number has reached N, then the order is complete, you can directly exit +m =p; A } at}
This is the practice of getting an SA array
However
An SA array is no use
Here is the algorithm for getting the height array
Specific proof can be referenced
Https://wenku.baidu.com/view/ed1be61e10a6f524ccbf85fd.html
This paper
It's very well written.
Just no comment ...
First, it's the code.
definition:rank[] Array: Opposite to sa[] array, rank[i] = J represents the rank of suffix starting with I
1 voidgetheight () {2 intK =0;3 for(inti =0; I < n; i++) Rank[sa[i] =i;4 for(inti =0; I < n; i++) {5 if(k) k--;//Because H[i] >= h[i-1]-1 (h[i] read the definition of the paper), so K needs-16 intj = Sa[rank[i]-1]; Get the position where the previous suffix begins7 while(S[i + K] = = S[j + K]) k++;//Compare from the back of K, until it is not the same8Height[rank[i]] =K;9 }Ten}
Suffix Array code detailed