CF1037H. Security

给出一个字符串$S$,给出$Q$个操作,给出$L, R, T$,求字典序最小的$S[l,r]$的子串,并且它的字典序严格大于$T$如果无解输出$−1$

贪心的去考虑一定是$T$的前缀(包括空串)+一个字母

建立$SAM$,暴力地跑前缀,如果前缀没有出现在$[l,r]$区间内,则$break$。否则对所有存在的前缀暴力枚举字母$[t[i+1]-‘a’+1,’z]$,而且越是长的前缀+字母越优。

  • 考虑$endpos$是表示集合里$S$出现的位置。即$trans[u][id]$搜到$endpos$的时候此时$S$出现的位置为$endpos$。初始化每个前缀出现的位置加到对应的点上,后缀链接建立树,树上启发式合并。预处理即可。
代码
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

#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 = 2e5 + 100;

struct seg
{
int l, r, sum;
} tree[SN * 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], n;
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 cur = ++scnt;
mxl[cur] = mxl[pre] + 1;
int u;
modify(rt[cur], 1, n, mxl[cur]);
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];
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 = head[x]; i; i = e[i].nxt)
{
int to = e[i].to;
dfs(to);
rt[x] = merge(rt[x], rt[to], 1, n);
}
}
void solve(int l, int r, char *t)
{
int u = 1;
int len = strlen(t + 1);
t[len + 1] = 'a' - 1;
int ansi = -1, ansj = -1;
for (int i = 1; i <= len + 1; i++)
{
//cout << t[i] << "---" << endl;
for (int j = t[i] - 'a' + 1; j < 26; j++)
{
int v = sam.trans[u][j];
if (v && query(rt[v], l + i - 1, r, 1, n)) // ?
{
//cout << i << " " << j << endl;
ansi = i;
ansj = j;
break;
}
}
if (i == len + 1)
break;
u = sam.trans[u][t[i] - 'a'];
if (!u || !query(rt[u], l + i - 1, r, 1, n))
break;
}
if (ansi == -1)
printf("-1\n");
else
{
// cout << ansi << endl;
for (int i = 1; i <= ansi - 1; i++)
printf("%c", t[i]);
printf("%c", ansj + 'a');
printf("\n");
}
}

char s[N], t[N];
int q;
int main()
{
scanf("%s", s + 1);
n = strlen(s + 1);
for (int i = 1; i <= n; i++)
sam.insert(s[i] - 'a');
for (int i = 2; i <= sam.scnt; i++)
addadge(sam.link[i], i);
//cout << sam.scnt << endl;
dfs(1);
scanf("%d", &q);
while (q--)
{
int l, r;
scanf("%d%d%s", &l, &r, t + 1);
solve(l, r, t);
}
}