CF666E Forensic Examination

给你一个串$S$以及一个字符串数组$T[1..m]$,$q$次询问,每次问$S$的子串$S[p_l..p_r]$在$T[l..r]$中的哪个串里的出现次数最多,并输出出现次数。

考虑求查询子串$[q_l,q_r]$在$T_i$出现的次数。和求最长公共子串一样,匹配不了往上跳$link$。对于找到的往上倍增跳$link$,使得公共长度维持在$\geq pr-pl+1$。(尽可能让$endpos$大),然后求当前$endpos$里出现次数。

多个子串可以建立广义后缀自动机,对于每个串建立最后权值线段树,维护$endpos$里出现次数最多的$i$,线段树合并。

  • 预处理$S[1..i]$,最多匹配的长度和位置。
  • 由于$maxlen[link[]]$满足单调性,可以倍增一下。

复杂度$O(|S|+Q\times (\log|T|+\log m)$

代码
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220

#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 = 1e6 + 10;
const int mod = 1e9 + 7;

const int SN = 1e6 + 100;

struct seg
{
int l, r, maxx;
int siz;
} tree[SN * 20];
int scnt;
void pushup(int pos)
{
tree[pos].maxx = (tree[ls].siz >= tree[rs].siz ? tree[ls].maxx : tree[rs].maxx);
tree[pos].siz = max(tree[ls].siz, tree[rs].siz);
}
void modify(int &pos, int l, int r, int w)
{
if (!pos)
pos = ++scnt;
if (l == r)
{
tree[pos].siz++;
tree[pos].maxx = l;
return;
}

int mid = (l + r) >> 1;
if (w <= mid)
modify(ls, l, mid, w);
else
modify(rs, mid + 1, r, w);
pushup(pos);
}
pii query(int pos, int ql, int qr, int l, int r)
{
if (ql <= l && r <= qr)
return mk(tree[pos].maxx, tree[pos].siz);
int mid = (l + r) >> 1;
pii res = mk(0, 0);
if (ql <= mid)
{
pii now = query(ls, ql, qr, l, mid);
if (now.second >= res.second)
res = now;
}
if (qr > mid)
{
pii now = query(rs, ql, qr, mid + 1, r);
if (now.second > res.second)
res = now;
}
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].siz = tree[u].siz + tree[v].siz;
tree[pos].maxx = l;
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], m;
struct SAM
{
int trans[SN][26];
int maxlen[SN], link[SN], pre, scnt;
SAM() { pre = scnt = 1; };
void init()
{
for (int j = 1; j <= scnt; j++)
{
link[j] = 0;
maxlen[j] = 0;
//siz[j] = 0;
memset(trans[j], 0, sizeof(trans[j]));
}
scnt = pre = 1;
};
void insert(int id, int p)
{
int cur = ++scnt;
maxlen[cur] = maxlen[pre] + 1;
modify(rt[cur], 1, m, p);
int u;

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 (maxlen[x] == maxlen[u] + 1)
link[cur] = x;
else
{
int nc = ++scnt;
link[nc] = link[x];
maxlen[nc] = maxlen[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;
int f[N][30];
struct edge
{
int to, nxt;
} e[SN];
int ecnt, head[SN];
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, m);
}
}

char s[N], t[N];
int ed[N], lp[N];
void prepare()
{
int u = 1, l = 0;
int slen = strlen(s + 1);
for (int i = 1; i <= slen; i++)
{
int id = s[i] - 'a';
while (u && !sam.trans[u][id])
{
u = sam.link[u];
l = sam.maxlen[u];
}
if (sam.trans[u][id])
u = sam.trans[u][id], l++;
else
u = 1, l = 0;
ed[i] = u, lp[i] = l;
}
// for (int i = 1; i <= slen; i++)
// cout << lp[i] << endl;
}
pii solve(int l, int r, int pl, int pr)
{
if (pr - pl + 1 > lp[pr])
return mk(l, 0);
int u = ed[pr];

for (int i = 19; i >= 0; i--)
if (f[u][i] && sam.maxlen[f[u][i]] >= pr - pl + 1)
u = f[u][i];

pii ans = query(rt[u], l, r, 1, m);
if (ans.second == 0)
return mk(l, 0);
else
return ans;
}
int main()
{
scanf("%s", s + 1);

scanf("%d", &m);
for (int i = 1; i <= m; i++)
{
scanf("%s", t + 1);
sam.pre = 1;
int le = strlen(t + 1);
for (int j = 1; j <= le; j++)
sam.insert(t[j] - 'a', i);
}
for (int j = 2; j <= sam.scnt; j++)
addadge(sam.link[j], j);
dfs(1);
prepare();
int q;
scanf("%d", &q);
while (q--)
{
int l, r, pl, pr;
scanf("%d %d %d %d", &l, &r, &pl, &pr);
pii ans = solve(l, r, pl, pr);
printf("%d %d\n", ans.first, ans.second);
}
}