yaoxi-std 的博客

$\text{开}\mathop{\text{卷}}\limits^{ju\check{a}n}\text{有益}$

0%

P4770 [NOI2018] 你的名字

P4770 [NOI2018] 你的名字

题面

题目链接

解法

容易想到离线后用后缀数组解决。(咱就是说能用SA的就不用SAM)

之后对于每个询问串,枚举其每个后缀$i$,注意到可以二分一个最大的出现过的子串$lcp$长度$len$,在SA上二分出左右端点,然后在主席树上判断区间$[l,r-len+1]$中是否存在$lcp\ge len$的数就好。这样时间复杂度是$O(n\log^2n)$的。但是$10^6$的数据肯定不能通过。

那么,既然是二分+主席树,能否在主席树上二分呢?事实上是可以的。所以复杂度瞬间降下一个$\log n$,同时写起来更加麻烦了。然而这种方法过于复杂了,我也没有完全看懂。有没有更加通俗易懂的单$\log$写法呢?

答案是有的。按顺序枚举每个后缀。如果$i$位置的长度为$len$,那么$i+1$位置的长度最小为$len-1$,原理和后缀$height$数组相同。这样暴力若干次$+1$并用主席树判断,一波势能分析下来就是$O(n\log n)$的。尽管如此但常数巨大,所以能看懂主席树二分的就尽量写主席树二分吧。

AC代码

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* @file: P4770.cpp
* @author: yaoxi-std
* @url: https://www.luogu.com.cn/problem/P4770
*/
// #pragma GCC optimize ("O2")
// #pragma GCC optimize ("Ofast", "inline", "-ffast-math")
// #pragma GCC target ("avx,sse2,sse3,sse4,mmx")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define resetIO(x) \
freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)
#define debug(fmt, ...) \
fprintf(stderr, "[%s:%d] " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)
template <class _Tp>
inline _Tp& read(_Tp& x) {
bool sign = false;
char ch = getchar();
long double tmp = 1;
for (; !isdigit(ch); ch = getchar())
sign |= (ch == '-');
for (x = 0; isdigit(ch); ch = getchar())
x = x * 10 + (ch ^ 48);
if (ch == '.')
for (ch = getchar(); isdigit(ch); ch = getchar())
tmp /= 10.0, x += tmp * (ch ^ 48);
return sign ? (x = -x) : x;
}
template <class _Tp>
inline void write(_Tp x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
write(x / 10);
putchar((x % 10) ^ 48);
}
const int MAXN = 2e6 + 10;
const int LOGN = 21;
const int INF = 0x3f3f3f3f3f3f3f3f;
struct SuffixArray {
int n, sa[MAXN], rk[MAXN], tp[MAXN], he[MAXN];
void radix_sort(int m) {
static int buc[MAXN];
for (int i = 0; i <= m; ++i)
buc[i] = 0;
for (int i = 1; i <= n; ++i)
buc[rk[i]]++;
for (int i = 1; i <= m; ++i)
buc[i] += buc[i - 1];
for (int i = n; i >= 1; --i)
sa[buc[rk[tp[i]]]--] = tp[i];
}
void init(int n, char* s) {
this->n = n;
int m = 200;
for (int i = 1; i <= n; ++i)
rk[i] = s[i] + 1, tp[i] = i;
radix_sort(m);
for (int w = 1, p = 0; p < n; m = p, w <<= 1) {
p = 0;
for (int i = 1; i <= w; ++i)
tp[++p] = n - w + i;
for (int i = 1; i <= n; ++i)
if (sa[i] > w)
tp[++p] = sa[i] - w;
radix_sort(m);
copy(rk + 1, rk + n + 1, tp + 1);
rk[sa[1]] = p = 1;
for (int i = 2; i <= n; ++i) {
if (tp[sa[i - 1]] == tp[sa[i]] && tp[sa[i - 1] + w] == tp[sa[i] + w])
rk[sa[i]] = p;
else
rk[sa[i]] = ++p;
}
}
for (int i = 1, k = 0; i <= n; ++i) {
if (k)
k--;
while (s[i + k] == s[sa[rk[i] - 1] + k])
k++;
he[rk[i]] = k;
}
}
} sa;
struct SegmentTree {
struct Node {
int sum, ls, rs;
} nd[MAXN * 25];
int tot, rt[MAXN];
int &operator[](int i) { return rt[i]; }
int newnode(int i) { return nd[++tot] = nd[i], tot; }
void update(int &i, int l, int r, int q, int v) {
i = newnode(i);
if (l == r)
return void(nd[i].sum += v);
int mid = (l + r) >> 1;
if (q <= mid)
update(nd[i].ls, l, mid, q, v);
else
update(nd[i].rs, mid + 1, r, q, v);
nd[i].sum = nd[nd[i].ls].sum + nd[nd[i].rs].sum;
}
int query(int u, int v, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr)
return nd[v].sum - nd[u].sum;
int mid = (l + r) >> 1, ret = 0;
if (ql <= mid)
ret += query(nd[u].ls, nd[v].ls, l, mid, ql, qr);
if (qr > mid)
ret += query(nd[u].rs, nd[v].rs, mid + 1, r, ql, qr);
return ret;
}
} sgt;
int n, m, q;
int typ[MAXN], pre[MAXN], nxt[MAXN], lst[MAXN];
int lg2[MAXN], st[MAXN][LOGN];
int ps[MAXN], ln[MAXN], ls[MAXN], rs[MAXN];
char s[MAXN], buf[MAXN], str[MAXN];
int getlcp(int l, int r) {
if (l == r)
return INF;
int k = lg2[r - l];
return min(st[l + 1][k], st[r - (1 << k) + 1][k]);
}
bool check(int p, int lt, int rt, int len) {
int l, r, cl, cr;
l = 1, r = p, cl = p;
while (l <= r) {
int mid = (l + r) >> 1;
if (getlcp(mid, p) >= len)
r = mid - 1, cl = mid;
else
l = mid + 1;
}
l = p, r = m;
while (l <= r) {
int mid = (l + r) >> 1;
if (getlcp(p, mid) >= len)
l = mid + 1, cr = mid;
else
r = mid - 1;
}
return sgt.query(sgt[lt - 1], sgt[rt - len + 1], 1, m, cl, cr) > 0;
}
signed main() {
scanf("%s", s + 1);
n = strlen(s + 1);
read(q);
m = n + 2;
copy(s + 1, s + n + 1, str + 1);
str[n + 1] = '$';
for (int i = 1; i <= q; ++i) {
scanf("%s", buf);
read(ls[i]), read(rs[i]);
ln[i] = strlen(buf);
copy(buf, buf + ln[i], str + m);
str[m + ln[i]] = '$';
fill(typ + m, typ + m + ln[i], i);
ps[i] = m, m += ln[i] + 1;
}
sa.init(m, str);
for (int i = 1; i <= n; ++i)
sgt.update(sgt[i] = sgt[i - 1], 1, m, sa.rk[i], 1);
for (int i = 2; i <= m; ++i)
lg2[i] = lg2[i >> 1] + 1;
for (int i = 1; i <= m; ++i)
st[i][0] = sa.he[i];
for (int j = 1; j < LOGN; ++j)
for (int i = 1; i + (1 << j) - 1 <= m; ++i)
st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
for (int i = 1; i <= m; ++i) {
pre[i] = lst[typ[sa.sa[i]]];
nxt[lst[typ[sa.sa[i]]]] = i;
lst[typ[sa.sa[i]]] = i;
}
for (int tt = 1; tt <= q; ++tt) {
int ans = 0;
for (int i = 1, k = 0; i <= ln[tt]; ++i) {
int len = min(rs[tt] - ls[tt] + 1, ps[tt] + ln[tt] - i);
int p = sa.rk[i + ps[tt] - 1];
if (k)
k--;
while (k < len && check(p, ls[tt], rs[tt], k + 1))
k++;
ans += ln[tt] - i + 1 - max(k, getlcp(pre[p], p));
}
write(ans), putchar('\n');
}
return 0;
}