Suffix array Study Notes

Source: Internet
Author: User

I. Arrays:
1. The suffix of suffix (I) string s starting from the I character is represented as suffix (I)

2. suffix array SA: It stores an arrangement of 1 .. n SA [1], SA [2],... , SA [n], and suffix (SA [I])

3. Ranking array Rank [I]: stores the "ranking" of Suffix (I) in ascending order of all suffixes ". That is, the character marked as I in string s. The Suffix (I) Starting with this character is the Suffix of Rank [I. If sa [I] = j, rank [j] = I. The relationship between the suffix array sa and rank is inverse.

4. height array: defines the longest common prefix of height [I] = suffix (sa [I-1]) and suffix (sa [I, that is, the longest common prefix of the two adjacent suffixes.

Ii. algorithm steps:

Multiplier Algorithm

I. Main Idea: multiply, s [I. I + 2 k? 1] is ranked by s [I. I + 2 k? 1? 1] and s [I + 2 k? 1. I + 2 k? 1.

Ii. Brief process: it is known that each length is 2 k? The ranking of 1 string can be used as the keyword xy, s [I. I + 2 k? For every string with 2 k length? 1] The first keyword x is s [I. I + 2 k? 1? 1]. The second keyword y is s [I + 2 k? 1. I + 2 k? 1. Take the aabaaaab string as an example:

1. k = 0. Sort the substrings whose names start with 20 = 1 and obtain rank [1 .. 8] =}

2. k = 1. Sort the substrings whose start length is 21 = 2. The rank keyword xy [1 .. 8] = {, 11, 11,}, get rank [1 .. 8] =}

3. k = 2. Sort the substrings whose names start with 22 = 4. The rank keyword xy [1 .. 8] = {14,21, 41,11, 12,13, 20,30}, get rank [1 .. 8] =}

4. k = 3. Sort the substrings whose names start with 23 = 8. The rank keyword xy [1 .. 8] = {,}, get rank [1 .. 8] =}

Note: In the sorting process, rank [] can have the same ranking, but the number of sa [] rows is not the same (just like the Excel sorting, sa is equivalent to the number, rank is equivalent to ranking ). This can be reflected in the program. It is recommended that you follow up the program experience.

Entire Process


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + 16LS4snPzbzW0NPJay8ytb1rIM/rim + tNO2 + left "left"> available from the height array. For j and k, set rank [j]


Void sorting (int j) // base sorting {memset (sum, 0, sizeof (sum); for (int I = 1; I <= s. size (); I ++) sum [rank [I + j] ++; for (int I = 1; I <= maxlen; I ++) sum [I] + = sum [I-1]; for (int I = s. size (); I> 0; I --) tsa [sum [rank [I + j] --] = I; // sorts the count of the second keyword, the suffix of tsa replacing sa with ranking I is tsa [I] memset (sum, 0, sizeof (sum); for (int I = 1; I <= s. size (); I ++) sum [rank [I] ++; for (int I = 1; I <= maxlen; I ++) sum [I] + = sum [I-1]; for (int I = s. size (); I> 0; I --) sa [sum [rank [tsa [I] --] = tsa [I]; // sort the first keyword count, construct reciprocal relationship} void get_sa () {int p; for (int I = 0; I
 
  
0; I --) sa [sum [trank [I] --] = I; rank [sa [1] = 1; for (int I = 2, p = 1; I <= s. size (); I ++) {if (trank [sa [I]! = Trank [sa [I-1]) p ++; rank [sa [I] = p;} // The first sa and rank construction is complete for (int j = 1; j <= s. size (); j * = 2) {sorting (j); trank [sa [1] = 1; p = 1; // replace rank for (int I = 2; I <= s. size (); I ++) {if (rank [sa [I]! = Rank [sa [I-1]) | (rank [sa [I] + j]! = Rank [sa [I-1] + j]) p ++; trank [sa [I] = p; // The space must be larger, at least 2 times} for (int I = 1; I <= s. size (); I ++) rank [I] = trank [I] ;}}
 

// Template of Jilin University # define N 1001 char s [N]; // N> 256int n, sa [N], height [N], rank [N], tmp [N], top [N]; void makesa () {// O (N * log N) int I, j, len, na; na = (n <256? 256: n); memset (top, 0, na * sizeof (int); for (I = 0; I <n; I ++) top [rank [I] = s [I] & 0xff] ++; for (I = 1; I <na; I ++) top [I] + = top [I-1]; for (I = 0; I <n; I ++) sa [-- top [rank [I] = I; for (len = 1; len <n; len <= 1) {for (I = 0; I <n; I ++) {j = sa [I]-len; if (j <0) j + = n; tmp [top [rank [j] ++] = j;} sa [tmp [top [0] = 0] = j = 0; for (I = 1; I <n; I ++) {if (rank [tmp [I]! = Rank [tmp [I-1] | rank [tmp [I] + len]! = Rank [tmp [I-1] + len]) top [+ + j] = I; sa [tmp [I] = j;} memcpy (rank, sa, n * sizeof (int); memcpy (sa, tmp, n * sizeof (int); if (j> = n-1) break;} void lcp () {// O (4 * N) int I, j, k; for (j = rank [height [I = k = 0] = 0]; I <n-1; I ++, k ++) while (k> = 0 & s [I]! = S [sa [J-1] + k]) height [j] = (k --), j = rank [sa [j] + 1];}

I personally use the code template of Wu zejun (Note is written by myself. If you have any questions, please advise):

/***************************** // WZJ P380 // suffix array + LCP + String Matching // note: 1. In this Code, k is a global variable. // 2. algorithm /*********************** * ******/# define MAXN 1001int n, k; // n = strlen (s); int Rank [MAXN]; int tmp [MAXN];/* sort sa by Rank */bool cmpSa (int I, int j) {if (Rank [I]! = Rank [j]) return Rank [I] <Rank [j]; Rank [t] Under else {/*, which must start with t and have a length less than or equal to k/2, sa [I] only has the suffix starting with I, but has different lengths */int ri = I + k <= n? Rank [I + k]:-1; int rj = j + k <= n? Rank [j + k]:-1; return ri
 
  

The above summary comes from:

Http://baike.baidu.com/link? Url = Response

Http://www.nocow.cn/index.php/%E5%90%8E%E7%BC%80%E6%95%B0%E7%BB%84

Recommended: http://www.cnblogs.com/staginner/archive/2012/02/02/2335600.html

And personal understanding


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.