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
| #include<bits/stdc++.h> using namespace std; const int N = 1e6 + 5; int n, m; char s[N]; int sa[N], x[N], y[N], c[N], rk[N], height[N]; void get_sa() { for(int i = 1;i <= n;i ++) c[x[i] = s[i]] ++; for(int i = 2;i <= m;i ++) c[i] += c[i - 1]; for(int i = n; i;i --) sa[c[x[i]] --] = i; for(int k = 1;k <= n;k <<= 1) { int num = 0; for(int i = n - k + 1;i <= n;i ++) y[ ++ num] = i; for(int i = 1;i <= n;i ++) { if(sa[i] > k) y[++ num] = sa[i] - k; } for(int i = 1;i <= m;i ++) c[i] = 0; for(int i = 1;i <= n;i ++) c[x[i]] ++; for(int i = 2;i <= m;i ++) c[i] += c[i - 1]; for(int i = n; i;i --) sa[c[x[y[i]]] --] = y[i], y[i] = 0; swap(x, y); x[sa[1]] = 1, num = 1; for(int i = 2;i <= n;i ++) x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) ? num : ++ num; if(num == n) break; m = num; } } void get_height() { for(int i = 1;i <= n;i ++) rk[sa[i]] = i; for(int i = 1, k = 0;i <= n;i ++) { if(rk[i] == 1) continue; if(k) k --; int j = sa[rk[i] - 1]; while(i + k <= n && j + k <= n && s[i + k] == s[j + k]) k ++; height[rk[i]] = k; } } int main() { scanf("%s", s + 1); n = strlen(s + 1), m = 122; get_sa(); get_height(); for(int i = 1;i <= n;i ++) printf("%d ", sa[i]); puts(""); for(int i = 1;i <= n;i ++) printf("%d ", height[i]); return 0; }
|