|
| 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 | + |
0 commit comments