Created
November 25, 2022 15:48
-
-
Save demidenko/14b34ad82e33fef3dfa1092c1e167e37 to your computer and use it in GitHub Desktop.
sufar nlogn slow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vector<int> sufar(string s) { | |
int n = size(s); | |
vector<int> p(n), c(n); | |
iota(begin(p), end(p), 0); | |
sort(begin(p), end(p), [&](int i, int j) { return s[i] < s[j]; }); | |
for(int i=1; i<n; ++i) c[p[i]] = c[p[i-1]] + (s[p[i]]!=s[p[i-1]]); | |
for(int k=1; k<n; k*=2) { | |
vector<vector<pair<int,int>>> f(n); | |
for(int i : p) { | |
int j = i - k; if(j < 0) j += n; | |
f[c[j]].emplace_back(c[i], j); | |
} | |
int cn = 0, i = 0; | |
for(auto &v : f) { | |
int x = -1; | |
for(auto &[ci, j] : v) { | |
p[i++] = j; | |
if(ci != x) ++cn, x = ci; | |
c[j] = cn - 1; | |
} | |
} | |
if(cn == n) break; | |
} | |
return p; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment