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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| const int MAXN = 50000 + 9;
int sa[MAXN], ht[MAXN], rk[MAXN]; bool t[MAXN * 2 + 1];
inline bool islms(const int i, const bool *t) { return i > 0 && t[i] && !t[i - 1]; }
template <typename T> inline void sort(T s, int *sa, const int len, const int sz, const int sigma, bool *t, int *b, int *cb, int *p) { memset(b, 0, sizeof(int) * sigma); memset(sa, -1, sizeof(int) * len); for (int i = 0; i < len; i++) b[s[i]]++; cb[0] = b[0]; for (int i = 1; i < sigma; i++) cb[i] = cb[i - 1] + b[i]; for (int i = sz - 1; i >= 0; i--) sa[--cb[s[p[i]]]] = p[i]; for (int i = 1; i < sigma; i++) cb[i] = cb[i - 1] + b[i - 1]; for (int i = 0; i < len; i++) if (sa[i] > 0 && !t[sa[i] - 1]) sa[cb[s[sa[i] - 1]]++] = sa[i] - 1; cb[0] = b[0]; for (int i = 1; i < sigma; i++) cb[i] = cb[i - 1] + b[i]; for (int i = len - 1; i >= 0; i--) if (sa[i] > 0 && t[sa[i] - 1]) sa[--cb[s[sa[i] - 1]]] = sa[i] - 1; }
template <typename T> void sais(T s, int *sa, const int len, bool *t, int *b, int *b1, const int sigma) { int i, j, x, p = -1, cnt = 0, sz = 0, *cb = b + sigma; for (t[len - 1] = 1, i = len - 2; i >= 0; i--) t[i] = s[i] < s[i + 1] || (s[i] == s[i + 1] && t[i + 1]); for (i = 1; i < len; i++) if (t[i] && !t[i - 1]) b1[sz++] = i; sort(s, sa, len, sz, sigma, t, b, cb, b1); for (i = sz = 0; i < len; i++) if (islms(sa[i], t)) sa[sz++] = sa[i]; for (i = sz; i < len; i++) sa[i] = -1; for (i = 0; i < sz; i++) { for (j = 0, x = sa[i]; j < len; j++) { if (p == -1 || s[x + j] != s[p + j] || t[x + j] != t[p + j]) { p = x; cnt++; break; } else if (j > 0 && (islms(x + j, t) || islms(p + j, t))) { break; } } sa[sz + (x >>= 1)] = cnt - 1; } for (i = j = len - 1; i >= sz; i--) if (sa[i] >= 0) sa[j--] = sa[i]; int *s1 = sa + len - sz, *b2 = b1 + sz; if (cnt < sz) sais(s1, sa, sz, t + len, b, b1 + sz, cnt); else for (i = 0; i < sz; i++) sa[s1[i]] = i; for (i = 0; i < sz; i++) b2[i] = b1[sa[i]]; sort(s, sa, len, sz, sigma, t, b, cb, b2); }
template <typename T> inline void getHeight(T s, const int n, int *sa, int *rk, int *ht) { for (int i = 1; i <= n; i++) rk[sa[i]] = i; for (int i = 0, j = 0, k = 0; i < n; ht[rk[i++]] = k) for (k ? k-- : 0, j = sa[rk[i] - 1]; s[i + k] == s[j + k]; k++) ; }
|