Skip to content

Commit 106757f

Browse files
committed
update README, and some indent,comment problem
1 parent d83ddaa commit 106757f

File tree

3 files changed

+314
-299
lines changed

3 files changed

+314
-299
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
Prefix Tree(Trie)
5858
Suffix Tree
5959
B-Tree
60+
Suffix Array
6061

6162
Hash by multiplication
6263
Hash table
@@ -96,5 +97,5 @@
9697
wycg1984: for K-Means
9798
xmuliang: for HeapSort, Kruskal MST
9899
wyh267: for base64, LRU, bubble sort, selection sort
99-
ZhangYou0122: Push-Relabel algorithm
100-
UsingtcNower: Suffix Array
100+
ZhangYou0122: Push-Relabel algorithm, Suffix Tree
101+
UsingtcNower: Suffix Array

include/suffix_array.h

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
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-
10-
*/
18+
* Please google SUFF_AR_ENG.pdf
19+
*
20+
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

2233
namespace alg {
2334
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+
}
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

5263
void SuffixArray::suffix_sort() {
@@ -56,7 +67,7 @@ namespace alg {
5667
// init bucket
5768
bucket.resize(ceil(log2(N))+1);
5869
for(size_t k=0;k<bucket.size();k++) bucket[k].resize(N+N);
59-
70+
6071
for(L=1,K=0;(L>>1)<N;L<<=1,K++) {
6172
sort(suffix.begin(), suffix.end(), bind(&SuffixArray::less_than, *this, placeholders::_1, placeholders::_2));
6273
update_bucket();
@@ -88,5 +99,4 @@ namespace alg {
8899
}
89100
}
90101

91-
#endif
92-
102+
#endif // __SUFFIX_ARRAY_H__

0 commit comments

Comments
 (0)