主要题意

求字符串$S$与$T$不同的子串总数

题解

先考虑$l = 1, r = |T|$的情况:

因为任意子串为字符串前缀的某些后缀,那么令$Lim[i]$表示$T[1…i]$在$S$上所能匹配的最大长度,$Posi[i]$表示$T$的后缀自动机上的点$i$的$endpos$集合中最靠前的位置,那么答案即为

$$Ans = \sum\limits_{i = 1}^{nodes} \max (0, Len[i] - min (Len[Father[i]], Lim[Posi[i]]))$$

接下来考虑$l, r$任意的情况:

原来能否在$S$的后缀自动机上往下走的判断依据只有当前节点是否存在$c$边,那么有了$l, r$的限制,就多需要判断向下的这个节点的$endpos$是否有存在于$[l + len, r]$($len$表示已匹配长度)区间的位置,$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
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

typedef long long LL;

const int MAXN = 2e06 + 10;
const int MAXL = 20 + 5;

const int INF = 0x7fffffff;

int N, M, Q;
char Orig[MAXN], Str[MAXN];

int Root[MAXN]= {0};
int Left[MAXN * MAXL]= {0}, Right[MAXN * MAXL]= {0};
int tnodes = 0;
void Modify (int& root, int left, int right, int pos) {
if (! root)
root = ++ tnodes;
if (left == right)
return ;
int mid = (left + right) >> 1;
pos <= mid ? Modify (Left[root], left, mid, pos) : Modify (Right[root], mid + 1, right, pos);
}
int Tree_Merge (int sl, int sr) {
if (! sl || ! sr)
return sl + sr;
int p = ++ tnodes;
Left[p] = Tree_Merge (Left[sl], Left[sr]);
Right[p] = Tree_Merge (Right[sl], Right[sr]);
return p;
}
bool Query (int root, int left, int right, int L, int R) {
if (! root || left > right)
return false;
if (L <= left && right <= R)
return true;
int mid = (left + right) >> 1;
if (L <= mid)
if (Query (Left[root], left, mid, L, R))
return true;
if (R > mid)
if (Query (Right[root], mid + 1, right, L, R))
return true;
return false;
}

struct SAM {
int Tree[MAXN][30];
int Father[MAXN];
int Len[MAXN], Posi[MAXN];
int last, nodes;

void init () {
for (int i = 1; i <= nodes; i ++) {
memset (Tree[i], 0, sizeof (Tree[i]));
Father[i] = 0, Posi[i] = 0;
}
last = nodes = 1;
}

void Append (int c, int pos, int type) {
int fa = last, p = ++ nodes;
last = p;
Len[p] = Len[fa] + 1, Posi[p] = pos;
while (fa && ! Tree[fa][c])
Tree[fa][c] = p, fa = Father[fa];
if (! fa)
Father[p] = 1;
else {
int x = Tree[fa][c];
if (Len[x] == Len[fa] + 1)
Father[p] = x;
else {
int np = ++ nodes;
Len[np] = Len[fa] + 1, Father[np] = Father[x], Posi[np] = Posi[x];
Father[p] = Father[x] = np;
memcpy (Tree[np], Tree[x], sizeof (Tree[x]));
while (fa && Tree[fa][c] == x)
Tree[fa][c] = np, fa = Father[fa];
}
}
if (type == 1)
Modify (Root[p], 1, N, pos);
}
int Topo[MAXN];
int Minp[MAXN];
int buck[MAXN];
/*void Merge_Minp () {
for (int i = 1; i <= nodes; i ++)
buck[i] = 0, Minp[i] = Posi[i];
for (int i = 1; i <= nodes; i ++)
buck[Len[i]] ++;
for (int i = 1; i <= N; i ++)
buck[i] += buck[i - 1];
for (int i = nodes; i >= 1; i --)
Topo[buck[Len[i]] --] = i;
for (int i = nodes; i >= 1; i --)
Minp[Father[Topo[i]]] = min (Minp[Father[Topo[i]]], Minp[Topo[i]]);
}*/
void Merge_Tree () {
for (int i = 1; i <= nodes; i ++)
buck[i] = 0;
for (int i = 1; i <= nodes; i ++)
buck[Len[i]] ++;
for (int i = 1; i <= N; i ++)
buck[i] += buck[i - 1];
for (int i = nodes; i >= 1; i --)
Topo[buck[Len[i]] --] = i;
for (int i = nodes; i >= 1; i --)
Root[Father[Topo[i]]] = Tree_Merge (Root[Father[Topo[i]]], Root[Topo[i]]);
}
} ;
SAM S, T;

int Lim[MAXN]= {0};
/*void Match (int l, int r) {
int p = 1, len = 0;
for (int i = 1; i <= M; i ++) {
int c = Str[i] - 'a';
while (p && ! S.Tree[p][c])
p = S.Father[p], len = S.Len[p];
while (p && ! Query (Root[S.Tree[p][c]], 1, N, l + len, r))
p = S.Father[p], len = S.Len[p];
S.Tree[p][c] ? (p = S.Tree[p][c], len ++) : (p = 1, len = 0);
Lim[i] = len;
}
}*/
void Work (int l, int r) {
int p = 1, len = 0;
for (int j = 1; j <= M; j ++) {
int c = Str[j] - 'a';
T.Append (c, j, 2);
while (true) {
if (S.Tree[p][c] && Query (Root[S.Tree[p][c]], 1, N, l + len, r)) {
p = S.Tree[p][c], len ++;
break;
}
if (! len)
break;
len --;
if (len == S.Len[S.Father[p]])
p = S.Father[p];
}
Lim[j] = len;
}
// T.Merge_Minp ();
// Match (l, r);
}

LL Solve () {
LL ans = 0;
for (int i = 1; i <= T.nodes; i ++)
ans += max (0, T.Len[i] - max (T.Len[T.Father[i]], Lim[T.Posi[i]]));
return ans;
}

int getnum () {
int num = 0;
char ch = getchar ();

while (! isdigit (ch))
ch = getchar ();
while (isdigit (ch))
num = (num << 3) + (num << 1) + ch - '0', ch = getchar ();

return num;
}

int main () {
scanf ("%s", Orig + 1);
N = strlen (Orig + 1);
S.init ();
for (int i = 1; i <= N; i ++)
S.Append (Orig[i] - 'a', i, 1);
S.Merge_Tree ();
scanf ("%d", & Q);
for (int i = 1; i <= Q; i ++) {
scanf ("%s", Str + 1);
int l = getnum (), r = getnum ();
T.init ();
M = strlen (Str + 1);
Work (l, r);
LL ans = Solve ();
printf ("%lld\n", ans);
}

return 0;
}

/*
scbamgepe
3
smape 1 9
sbape 1 9
sgepe 1 9
*/

/*
scbamgepe
3
smape 2 7
sbape 3 8
sgepe 1 9
*/