1- /*
2- * suffix array algorithm
1+ /* ******************************************************************************
2+ * ALGORITHM IMPLEMENTAIONS
3+ *
4+ * /\ | _ _ ._ o _|_ |_ ._ _ _
5+ * /--\ | (_| (_) | | |_ | | | | | _>
6+ * _|
7+ *
8+ * SUFFIX ARRAY
9+ *
10+ * Features:
11+ * suffix array can sort all the suffixs in time complexity O(n*log^2(n)),
12+ * and use memory in O(n). And suffix array can get two suffixs' longest
13+ * common prefix(lcp) in O(log(n)) complexity.
314 *
4- * Features: suffix array can sort all the suffixs in time complexity O(n*log^2(n)), and use memory in O(n).
5- * And suffix array can get two suffixs' longest common prefix(lcp) in O(log(n)) complexity.
615 * You can test it by running suffix_array_demo.cpp
7- * Want to get more detailed information about suffix array? Please google SUFF_AR_ENG.pdf
16+ * Want to get more detailed information about suffix array?
817 *
9- * author: nowerzt@gmail.com
10- */
18+ * Please google SUFF_AR_ENG.pdf
19+ *
20+ * AUTHOR: nowerzt@gmail.com
21+ ******************************************************************************/
1122
12- #ifndef _SUFFIX_ARRAY_H
13- #define _SUFFIX_ARRAY_H
23+ #ifndef __SUFFIX_ARRAY_H__
24+ #define __SUFFIX_ARRAY_H__
1425
1526#include < algorithm>
1627#include < vector>
@@ -21,32 +32,32 @@ using namespace std;
2132
2233namespace alg {
2334class SuffixArray {
24- private:
25- vector<vector<int > > bucket;
26- vector<int > suffix;
27- int N, L, K;
28- const string& str;
29- void suffix_sort ();
30- void update_bucket ();
31-
32- bool less_than (int a, int b) {
33- if (K==0 ) return str[a]<str[b];
34- else {
35- if (bucket[K-1 ][a]==bucket[K-1 ][b]) return bucket[K-1 ][a+L/2 ]<bucket[K-1 ][b+L/2 ];
36- else return bucket[K-1 ][a]<bucket[K-1 ][b];
35+ private:
36+ vector<vector<int > > bucket;
37+ vector<int > suffix;
38+ int N, L, K;
39+ const string& str;
40+ void suffix_sort ();
41+ void update_bucket ();
42+
43+ bool less_than (int a, int b) {
44+ if (K==0 ) return str[a]<str[b];
45+ else {
46+ if (bucket[K-1 ][a]==bucket[K-1 ][b]) return bucket[K-1 ][a+L/2 ]<bucket[K-1 ][b+L/2 ];
47+ else return bucket[K-1 ][a]<bucket[K-1 ][b];
48+ }
3749}
38- }
3950
40- bool equal (int a, int b) {
41- return !less_than (a,b) && !less_than (b,a);
42- }
51+ bool equal (int a, int b) {
52+ return !less_than (a,b) && !less_than (b,a);
53+ }
4354
44- public:
45- explicit SuffixArray (const string& s) : N(s.size()), L(0 ), K(0 ), str(s) { suffix_sort ();}
46- // return the sorted suffix
47- int operator [] (int i) { return suffix[i];}
48- // Given two suffixs of string, return the longest common prefix length
49- int lcp_length (int x, int y);
55+ public:
56+ explicit SuffixArray (const string& s) : N(s.size()), L(0 ), K(0 ), str(s) { suffix_sort ();}
57+ // return the sorted suffix
58+ int operator [] (int i) { return suffix[i];}
59+ // Given two suffixs of string, return the longest common prefix length
60+ int lcp_length (int x, int y);
5061};
5162
5263void SuffixArray::suffix_sort () {
@@ -56,7 +67,7 @@ namespace alg {
5667// init bucket
5768bucket.resize (ceil (log2 (N))+1 );
5869for (size_t k=0 ;k<bucket.size ();k++) bucket[k].resize (N+N);
59-
70+
6071for (L=1 ,K=0 ;(L>>1 )<N;L<<=1 ,K++) {
6172sort (suffix.begin (), suffix.end (), bind (&SuffixArray::less_than, *this , placeholders::_1, placeholders::_2));
6273update_bucket ();
@@ -88,5 +99,4 @@ namespace alg {
8899}
89100}
90101
91- #endif
92-
102+ #endif // __SUFFIX_ARRAY_H__
0 commit comments