题目链接
https://codeforces.com/gym/103427/problem/M
题目大意
给定小写字母构成的字符串 \(S\)
,对于 \(S\)
的每个前缀,求该前缀的所有子串中字典序最大且出现位置最靠左的子串的左右端点。
数据范围
解题思路
参考代码
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 70 71 72 73 74 75 76 77
| const int MAXN = 1e6+10; const int INF = 0x3f3f3f3f;
using SA :: sa; using SA :: rk; using SA :: height; int n; char s[MAXN]; int st[MAXN][21]; int h[MAXN], pos[MAXN]; int lg2[MAXN]; multiset<int> S; int query(int l, int r) { int s = lg2[r - l + 1]; return min(st[l][s], st[r - (1 << s) + 1][s]); } int qlmax(int x) { auto it = S.lower_bound(x); if (it == S.begin()) { return -1; } else { return *(--it); } } int main() { scanf("%s", s + 1); n = strlen(s + 1); SA::init(s, n); lg2[1] = 0; for (int i = 2; i <= n; i++) { lg2[i] = lg2[i >> 1] + 1; } for (int i = 1; i <= n; i++) { st[i][0] = height[i]; } for (int j = 1; j <= 20; j++) { for (int i = 1; i + (1 << j) - 1 <= n; i++) { st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); } } h[rk[1]] = 0; S.insert(rk[1]); for (int i = 2; i <= n; i++) { int r = rk[i]; int l = qlmax(r); if (l == -1) { h[r] = 0; } else { h[r] = query(l + 1, r); } S.insert(r); } for (int i = n, r = n; i >= 1; i--) { int x = sa[i] + h[i]; while (r >= x) { pos[r] = sa[i]; r--; } } for (int i = 1; i <= n; i++) { printf("%d %d\n", pos[i], i); } return 0; }
|