#include #include #include #include #include #include #include #include #include #include using namespace std; //Suffix Array SA-IS algorithm //O(N), using too many memory //verified 2015/03/24 03:30 at // https://www.hackerrank.com/contests/w7/challenges/string-function-calculation // http://codeforces.com/contest/529/problem/A class SA_IS{ //compress alphabets //return vector vector compress(vector& v){ set alph(v.begin(), v.end()); vector dic(alph.begin(), alph.end()); vector bucket_size(dic.size(),0); for(int i=0; i sa; vector lcp; struct substring{ int pos; int size; vector::iterator str; vector::iterator ls; substring(){ } substring(const int pos, const int size, const vector::iterator& str, const vector::iterator& ls){ this->pos = pos; this->size = size; this->str = str; this->ls = ls; } bool operator<(const substring& x) const { for(int i=0; isize, x.size); i++){ if(this->str[i] != x.str[i]){ return this->str[i] < x.str[i]; }else if(this->ls[i] != x.ls[i]){ return this->ls[i] == SA_IS::L; } } return this->size < x.size; } bool operator==(const substring& x) const { for(int i=0; isize, x.size); i++){ if(this->str[i] != x.str[i]){ return false; }else if(this->ls[i] != x.ls[i]){ return false; } } return this->size == x.size; } substring operator=(const substring& x){ this->pos = x.pos; this->size = x.size; this->str = x.str; this->ls = x.ls; return *this; } }; vector rec(vector& v){ v.push_back(-1); vector bucket_size = compress(v); //bucket vector ret(v.size(), -1); vector::iterator> bucket(bucket_size.size()); vector fixed(v.size(), false); vector::iterator> bucket_fixed(bucket_size.size()); vector bucket_front(bucket_size.size()); vector bucket_back(bucket_size.size()); vector used(v.size(), false); //initialize { int sum = 0; for(int i=0; i LS(v.size()); LS.back() = S; for(int i=v.size()-2; i>=0; i--){ if(v[i] < v[i+1]) LS[i] = S; else if (v[i] > v[i+1]) LS[i] = L; else LS[i] = LS[i+1]; } vector LMS; for(int i=1; i > LMS_substring; for(int i=0; i 0){ vector< pair> LMS_substring_old = LMS_substring; sort(LMS_substring.begin(), LMS_substring.end()); vector LMS_order(LMS_substring.size()); int cnt = 0; LMS_order[ LMS_substring[0].second ] = cnt; cnt++; for(int i=1; i new_order = rec(LMS_order); LMS_order = vector(new_order.begin()+1, new_order.end()); for(int i=0; i=0; i--){ int c = v[LMS_substring[i].first.pos]; bucket[c][bucket_back[c]] = LMS_substring[i].first.pos; bucket_back[c]--; } } for(int i=0; i=0; i--){ for(int j=bucket_size[i]-1; j>=0; j--){ if( bucket[i][j] - 1 < 0 ) continue; if( LS[ bucket[i][j]-1 ] == S ){ if(used[bucket[i][j]-1]) continue; used[ bucket[i][j]-1 ] = true; int c = v[ bucket[i][j]-1 ]; bucket[c][ bucket_back[c] ] = bucket[i][j]-1; bucket_fixed[c][ bucket_back[c] ] = true; bucket_back[c]--; } } } ret[0] = ret.size()-1; return ret; } vector rec(string &s){ vector v(s.begin(), s.end()); return rec(v); } void constract_lcp(){ int n = my_s.size(); lcp.resize(n+1); vector rank(n+1); for(int i=0; i<=n; i++) rank[sa[i]] = i; int h=0; lcp[0] = 0; for(int i=0; i0) h--; for(; j+h> n; vector s(n); for(int i=0; i> s[i]; s[i] += "{"; } vector begin(n); string my_s = ""; for(int i=0; i order(my_s.size()); for(int i=1; i<=my_s.size(); i++){ order[sais.sa[i]] = i; } priority_queue, greater> pq; for(int i=0; i