Skip to content

Commit

Permalink
update README, and some indent,comment problem
Browse files Browse the repository at this point in the history
  • Loading branch information
xtaci committed May 21, 2014
1 parent d83ddaa commit 106757f
Show file tree
Hide file tree
Showing 3 changed files with 314 additions and 299 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
Prefix Tree(Trie)
Suffix Tree
B-Tree
Suffix Array

Hash by multiplication
Hash table
Expand Down Expand Up @@ -96,5 +97,5 @@
wycg1984: for K-Means
xmuliang: for HeapSort, Kruskal MST
wyh267: for base64, LRU, bubble sort, selection sort
ZhangYou0122: Push-Relabel algorithm
UsingtcNower: Suffix Array
ZhangYou0122: Push-Relabel algorithm, Suffix Tree
UsingtcNower: Suffix Array
80 changes: 45 additions & 35 deletions include/suffix_array.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
/*
* suffix array algorithm
/*******************************************************************************
* ALGORITHM IMPLEMENTAIONS
*
* /\ | _ _ ._ o _|_ |_ ._ _ _
* /--\ | (_| (_) | | |_ | | | | | _>
* _|
*
* SUFFIX ARRAY
*
* Features:
* suffix array can sort all the suffixs in time complexity O(n*log^2(n)),
* and use memory in O(n). And suffix array can get two suffixs' longest
* common prefix(lcp) in O(log(n)) complexity.
*
* Features: suffix array can sort all the suffixs in time complexity O(n*log^2(n)), and use memory in O(n).
* And suffix array can get two suffixs' longest common prefix(lcp) in O(log(n)) complexity.
* You can test it by running suffix_array_demo.cpp
* Want to get more detailed information about suffix array? Please google SUFF_AR_ENG.pdf
* Want to get more detailed information about suffix array?
*
* author: nowerzt@gmail.com
*/
* Please google SUFF_AR_ENG.pdf
*
* AUTHOR: nowerzt@gmail.com
******************************************************************************/

#ifndef _SUFFIX_ARRAY_H
#define _SUFFIX_ARRAY_H
#ifndef __SUFFIX_ARRAY_H__
#define __SUFFIX_ARRAY_H__

#include <algorithm>
#include <vector>
Expand All @@ -21,32 +32,32 @@ using namespace std;

namespace alg {
class SuffixArray {
private:
vector<vector<int> > bucket;
vector<int> suffix;
int N, L, K;
const string& str;
void suffix_sort();
void update_bucket();

bool less_than(int a, int b) {
if(K==0) return str[a]<str[b];
else {
if(bucket[K-1][a]==bucket[K-1][b]) return bucket[K-1][a+L/2]<bucket[K-1][b+L/2];
else return bucket[K-1][a]<bucket[K-1][b];
private:
vector<vector<int> > bucket;
vector<int> suffix;
int N, L, K;
const string& str;
void suffix_sort();
void update_bucket();

bool less_than(int a, int b) {
if(K==0) return str[a]<str[b];
else {
if(bucket[K-1][a]==bucket[K-1][b]) return bucket[K-1][a+L/2]<bucket[K-1][b+L/2];
else return bucket[K-1][a]<bucket[K-1][b];
}
}
}

bool equal(int a, int b) {
return !less_than(a,b) && !less_than(b,a);
}
bool equal(int a, int b) {
return !less_than(a,b) && !less_than(b,a);
}

public:
explicit SuffixArray(const string& s) : N(s.size()), L(0), K(0), str(s) { suffix_sort();}
// return the sorted suffix
int operator [] (int i) { return suffix[i];}
// Given two suffixs of string, return the longest common prefix length
int lcp_length(int x, int y);
public:
explicit SuffixArray(const string& s) : N(s.size()), L(0), K(0), str(s) { suffix_sort();}
// return the sorted suffix
int operator [] (int i) { return suffix[i];}
// Given two suffixs of string, return the longest common prefix length
int lcp_length(int x, int y);
};

void SuffixArray::suffix_sort() {
Expand All @@ -56,7 +67,7 @@ namespace alg {
// init bucket
bucket.resize(ceil(log2(N))+1);
for(size_t k=0;k<bucket.size();k++) bucket[k].resize(N+N);

for(L=1,K=0;(L>>1)<N;L<<=1,K++) {
sort(suffix.begin(), suffix.end(), bind(&SuffixArray::less_than, *this, placeholders::_1, placeholders::_2));
update_bucket();
Expand Down Expand Up @@ -88,5 +99,4 @@ namespace alg {
}
}

#endif

#endif // __SUFFIX_ARRAY_H__
Loading

0 comments on commit 106757f

Please sign in to comment.