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.
3
14
*
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.
6
15
* 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?
8
17
*
9
-
10
- */
18
+ * Please google SUFF_AR_ENG.pdf
19
+ *
20
+
21
+ ******************************************************************************/
11
22
12
- #ifndef _SUFFIX_ARRAY_H
13
- #define _SUFFIX_ARRAY_H
23
+ #ifndef __SUFFIX_ARRAY_H__
24
+ #define __SUFFIX_ARRAY_H__
14
25
15
26
#include < algorithm>
16
27
#include < vector>
@@ -21,32 +32,32 @@ using namespace std;
21
32
22
33
namespace alg {
23
34
class 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
+ }
37
49
}
38
- }
39
50
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
+ }
43
54
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);
50
61
};
51
62
52
63
void SuffixArray::suffix_sort () {
@@ -56,7 +67,7 @@ namespace alg {
56
67
// init bucket
57
68
bucket.resize (ceil (log2 (N))+1 );
58
69
for (size_t k=0 ;k<bucket.size ();k++) bucket[k].resize (N+N);
59
-
70
+
60
71
for (L=1 ,K=0 ;(L>>1 )<N;L<<=1 ,K++) {
61
72
sort (suffix.begin (), suffix.end (), bind (&SuffixArray::less_than, *this , placeholders::_1, placeholders::_2));
62
73
update_bucket ();
@@ -88,5 +99,4 @@ namespace alg {
88
99
}
89
100
}
90
101
91
- #endif
92
-
102
+ #endif // __SUFFIX_ARRAY_H__
0 commit comments