P4094 [HEOI2016/TJOI2016]字符串

求$s[l_1..r_1]$中所有子串和$s[l_2..r_2]$的最长公共前缀

模版题。看清楚题目,答案具有单调性,二分长度,利用倍增和线段树合并查询下是否存在子串即可。

代码
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
192
193
194

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mk make_pair
#define ls tree[pos].l
#define rs tree[pos].r
const int N = 2e5 + 10;
const int mod = 1e9 + 7;

const int SN = 2e5 + 100;
int n;
struct seg
{
int l, r, sum;
} tree[N * 20];
int scnt;
void pushup(int pos)
{
tree[pos].sum = tree[ls].sum + tree[rs].sum;
}
void modify(int &pos, int l, int r, int w)
{
if (!pos)
pos = ++scnt;
if (l == r)
{
tree[pos].sum++;
return;
}

int mid = (l + r) >> 1;
if (w <= mid)
modify(ls, l, mid, w);
else
modify(rs, mid + 1, r, w);
pushup(pos);
}
int query(int pos, int ql, int qr, int l, int r)
{
if (ql <= l && r <= qr)
return tree[pos].sum;
int mid = (l + r) >> 1;
int res = 0;
if (ql <= mid)
res += query(ls, ql, qr, l, mid);
if (qr > mid)
res += query(rs, ql, qr, mid + 1, r);
return res;
}

int merge(int u, int v, int l, int r)
{
if (!u || !v)
return u + v;
int pos = ++scnt;
if (l == r)
{
tree[pos].sum = tree[u].sum + tree[v].sum;
return pos;
}
int mid = (l + r) >> 1;
ls = merge(tree[u].l, tree[v].l, l, mid);
rs = merge(tree[u].r, tree[v].r, mid + 1, r);
pushup(pos);
return pos;
}

int rt[SN];
struct SAM
{
int trans[SN][26];
int mxl[SN], link[SN], pre, scnt;
SAM() { pre = scnt = 1; };
void init()
{
for (int j = 1; j <= scnt; j++)
{
link[j] = 0;
mxl[j] = 0;
//siz[j] = 0;
memset(trans[j], 0, sizeof(trans[j]));
}
scnt = pre = 1;
};
void insert(int id, int p)
{
int cur = ++scnt;
mxl[cur] = mxl[pre] + 1;
int u;
modify(rt[cur], 1, n, p);
for (u = pre; u && !trans[u][id]; u = link[u])
trans[u][id] = cur;
pre = cur;
if (!u)
link[cur] = 1;
else
{
int x = trans[u][id];
if (mxl[x] == mxl[u] + 1)
link[cur] = x;
else
{
int nc = ++scnt;
link[nc] = link[x];
mxl[nc] = mxl[u] + 1;
memcpy(trans[nc], trans[x], sizeof(trans[x]));

link[cur] = link[x] = nc;
for (; u && trans[u][id] == x; u = link[u])
trans[u][id] = nc;
}
}
}
} sam;

struct edge
{
int to, nxt;
} e[SN];
int ecnt, head[SN], f[SN][21];
void addadge(int u, int v)
{
e[++ecnt].to = v;
e[ecnt].nxt = head[u];
head[u] = ecnt;
}
void dfs(int x)
{
for (int i = 1; i <= 20; i++)
f[x][i] = f[f[x][i - 1]][i - 1];
for (int i = head[x]; i; i = e[i].nxt)
{

int to = e[i].to;
f[to][0] = x;
dfs(to);
rt[x] = merge(rt[x], rt[to], 1, n);
}
}

char s[N];
int ed[N];
int m;

bool check(int l, int r, int ll, int rr, int x)
{
if (x == 0)
return 1;
int u = ed[ll + x - 1];
//cout << u << endl;
for (int i = 20; i >= 0; i--)
if (f[u][i] && sam.mxl[f[u][i]] >= x)
u = f[u][i];
//cout << u << endl;
return query(rt[u], l + x - 1, r, 1, n) > 0;
}
int main()
{
scanf("%d%d", &n, &m);
scanf("%s", s + 1);
for (int i = 1; i <= n; i++)
sam.insert(s[i] - 'a', i), ed[i] = sam.pre;
for (int i = 2; i <= sam.scnt; i++)
addadge(sam.link[i], i);
///out << sam.scnt << endl;

dfs(1);

while (m--)
{
int l1, r1, l2, r2;
scanf("%d%d%d%d", &l1, &r1, &l2, &r2);
int l = 0;
int r = min(r2 - l2 + 1, r1 - l1 + 1);
int ans = 0;
// cout << "---" << endl;
// cout << check(l1, r1, l2, r2, 0) << endl;
while (l <= r)
{
//cout << l << " " << r << endl;
int mid = (l + r) >> 1;
if (check(l1, r1, l2, r2, mid))
{
ans = mid;
l = mid + 1;
}
else
r = mid - 1;
}
printf("%d\n", ans);
}
}