Skip to content

Commit

Permalink
Shorten SuffixArray (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
lrvideckis authored Oct 23, 2024
1 parent 3a59551 commit 0f7058c
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions content/strings/SuffixArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct SuffixArray {
vi sa, lcp;
SuffixArray(string& s, int lim=256) { // or basic_string<int>
int n = sz(s) + 1, k = 0, a, b;
vi x(all(s)), y(n), ws(max(n, lim)), rank(n);
vi x(all(s)), y(n), ws(max(n, lim));
x.push_back(0), sa = lcp = y, iota(all(sa), 0);
for (int j = 0, p = 0; p < n; j = max(1, j * 2), lim = p) {
p = j, iota(all(y), n - j);
Expand All @@ -34,9 +34,8 @@ struct SuffixArray {
rep(i,1,n) a = sa[i - 1], b = sa[i], x[b] =
(y[a] == y[b] && y[a + j] == y[b + j]) ? p - 1 : p++;
}
rep(i,1,n) rank[sa[i]] = i;
for (int i = 0, j; i < n - 1; lcp[rank[i++]] = k)
for (k && k--, j = sa[rank[i] - 1];
for (int i = 0, j; i < n - 1; lcp[x[i++]] = k)
for (k && k--, j = sa[x[i] - 1];
s[i + k] == s[j + k]; k++);
}
};

0 comments on commit 0f7058c

Please sign in to comment.