Skip to content

Commit cc14358

Browse files
committed
Merge branch 'master' into develop
2 parents 245cdd0 + 73c6f97 commit cc14358

File tree

4 files changed

+134
-4
lines changed

4 files changed

+134
-4
lines changed

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ CC=gcc
33
CPP=g++
44
AR=ar
55
RANLIB=ranlib
6-
CFLAGS= -g -Wall -Wno-unused-function
6+
CFLAGS= -g -Wall -Wno-unused-function -std=gnu++0x
77
SRCDIR = ./src
88
INCLUDEDIR = -I./include -I.
99
DEPS =
@@ -68,7 +68,8 @@ PROGRAMS = m_based_demo \
6868
bubble_sort_demo \
6969
selection_sort_demo \
7070
8queue_demo \
71-
palindrome_demo
71+
palindrome_demo \
72+
suffix_array_demo
7273

7374
all: $(PROGRAMS)
7475

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
Red-black tree
5656
Interval tree
5757
Prefix Tree(Trie)
58-
*Suffix Tree(未实现)*
58+
Suffix Tree
5959
B-Tree
6060

6161
Hash by multiplication
@@ -96,4 +96,4 @@
9696
wycg1984: for K-Means
9797
xmuliang: for HeapSort, Kruskal MST
9898
wyh267: for base64, LRU, bubble sort, selection sort
99-
ZhangYou0122 : Push-Relabel algorithm
99+
ZhangYou0122 : Push-Relabel algorithm, SuffixTree

include/suffix_array.h

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* suffix array algorithm
3+
*
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+
* 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
8+
*
9+
10+
*/
11+
12+
#ifndef _SUFFIX_ARRAY_H
13+
#define _SUFFIX_ARRAY_H
14+
15+
#include <algorithm>
16+
#include <vector>
17+
#include <string>
18+
#include <math.h>
19+
20+
using namespace std;
21+
22+
namespace alg {
23+
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];
37+
}
38+
}
39+
40+
bool equal(int a, int b) {
41+
return !less_than(a,b) && !less_than(b,a);
42+
}
43+
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);
50+
};
51+
52+
void SuffixArray::suffix_sort() {
53+
// init suffix
54+
suffix.resize(N);
55+
for(int i=0;i<N;i++) suffix[i]=i;
56+
// init bucket
57+
bucket.resize(ceil(log2(N))+1);
58+
for(size_t k=0;k<bucket.size();k++) bucket[k].resize(N+N);
59+
60+
for(L=1,K=0;(L>>1)<N;L<<=1,K++) {
61+
sort(suffix.begin(), suffix.end(), bind(&SuffixArray::less_than, *this, placeholders::_1, placeholders::_2));
62+
update_bucket();
63+
}
64+
}
65+
66+
67+
void SuffixArray::update_bucket() {
68+
int seq=0;
69+
bucket[K][suffix[0]]=0;
70+
for(int i=1;i<N;i++) {
71+
if(!equal(suffix[i],suffix[i-1])) seq++;
72+
bucket[K][suffix[i]]=seq;
73+
}
74+
fill(bucket[K].begin()+N, bucket[K].end(), -1);
75+
}
76+
77+
int SuffixArray::lcp_length(int x, int y) {
78+
if(x==y) return N-x;
79+
int ret=0;
80+
for(int k=K-1;k>=0 && x<N && y<N;k--) {
81+
if(bucket[k][x]==bucket[k][y]) {
82+
x += (1<<k);
83+
y += (1<<k);
84+
ret += (1<<k);
85+
}
86+
}
87+
return ret;
88+
}
89+
}
90+
91+
#endif
92+

src/suffix_array_demo.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <math.h>
4+
5+
#include "suffix_array.h"
6+
7+
using namespace std;
8+
using namespace alg;
9+
10+
void print(string::iterator b, string::iterator e) {
11+
for(auto it=b;it!=e;++it) cout<<*it;
12+
}
13+
14+
int main()
15+
{
16+
string str;
17+
while(cin>>str) {
18+
SuffixArray sa(str);
19+
cout<<endl;
20+
cout<<"sorted suffixs are:"<<endl;
21+
for(size_t i=0;i<str.size();i++) {
22+
print(str.begin()+sa[i], str.end());
23+
cout<<endl;
24+
}
25+
cout<<endl;
26+
cout<<"The length of the longest common prefix of two suffixs ";
27+
int i=rand()%str.size();
28+
int j=rand()%str.size();
29+
print(str.begin()+i,str.end());
30+
cout<<" and ";
31+
print(str.begin()+j,str.end());
32+
cout<<" is ";
33+
cout<<sa.lcp_length(i,j)<<endl;
34+
cout<<endl;
35+
}
36+
return 0;
37+
}

0 commit comments

Comments
 (0)