-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuffixarr.sublime-snippet
31 lines (31 loc) · 1.25 KB
/
suffixarr.sublime-snippet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<snippet>
<content><![CDATA[
struct SuffixArray {
vector<int> sa, lcp;
SuffixArray(string& s, int lim=256) {
int n = s.size() + 1, k = 0, a, b;
vector<int> x(all(s)+1), y(n), ws(max(n, lim)), rank(n);
sa = lcp = y, iota(all(sa), 0);
for (int j = 0, p = 0; p < n; j = max(1LL, j * 2), lim = p) {
p = j, iota(all(y), n - j);
for(int i=0;i<n;i++) if (sa[i] >= j) y[p++] = sa[i] - j;
fill(all(ws), 0);
for(int i=0;i<n;i++) ws[x[i]]++;
for(int i=1;i<lim;i++) ws[i] += ws[i - 1];
for (int i = n; i--;) sa[--ws[x[y[i]]]] = y[i];
swap(x, y), p = 1, x[sa[0]] = 0;
for(int i=1;i<n;i++) a = sa[i - 1], b = sa[i], x[b] =
(y[a] == y[b] && y[a + j] == y[b + j]) ? p - 1 : p++;
}
for(int i=1;i<n;i++) rank[sa[i]] = i;
for (int i = 0, j; i < n - 1; lcp[rank[i++]] = k)
for (k && k--, j = sa[rank[i] - 1];
s[i + k] == s[j + k]; k++);
}
};
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>_suffixarr</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>