#include <stdio.h>
#include <string.h>
#define N 300000
#define M 10000
#define L 500000
#define N_ (L + 2)
#define A 26
int tt[N_][A], ff_[N_][A], ff[N_], dd[N_], n_;
void build(char **ccc, int m) {
static int qu[N_];
int cnt, l, g, h, a, s, t, e, f;
n_ = 2;
dd[0] = -1, dd[1] = 0;
for (h = 0; h < m; h++) {
l = strlen(ccc[h]);
for (g = 0, s = 1; g < l; g++) {
a = ccc[h][g] - 'a';
if (!tt[s][a])
dd[tt[s][a] = n_++] = g + 1;
s = tt[s][a];
}
}
cnt = 0;
for (a = 0; a < A; a++)
tt[0][a] = 1, ff_[0][a] = 0;
ff[1] = 0, qu[cnt++] = 1;
for (h = 0; h < cnt; h++) {
s = qu[h], e = ff[s];
for (a = 0; a < A; a++) {
t = tt[s][a], f = tt[e][a];
if (t == 0)
tt[s][a] = f, ff_[s][a] = s;
else
ff_[s][a] = ff_[e][a], ff[t] = f, qu[cnt++] = t;
}
}
}
int main() {
static char aa[N + 1], bb[L + 1], *ccc[M], rank[N + 1];
static int rr[N + 1], jj[N + 1], jj_[N + 1], qu[N + 1];
int n, m, q, cnt, h, i, j, a, s, t, lower, upper;
scanf("%d%d", &m, &q);
for (h = 0; h < m; h++) {
scanf("%s", bb);
ccc[h] = strdup(bb);
}
scanf("%s", aa), n = strlen(aa);
build(ccc, m);
for (i = 0, s = 1; i < n; i++) {
a = aa[i] - 'a';
for (t = s; t = ff_[t][a]; t = ff[t])
rr[i - dd[t]] = i;
s = tt[s][a];
}
while (dd[s] >= 0)
rr[n - dd[s]] = n, s = ff[s];
cnt = 0;
for (i = n; i >= 0; i--) {
while (cnt && rr[qu[cnt - 1]] <= rr[i])
cnt--;
qu[cnt++] = i;
lower = -1, upper = cnt - 1;
while (upper - lower > 1) {
h = (lower + upper) / 2;
if (qu[h] <= rr[i])
upper = h;
else
lower = h;
}
jj[i] = qu[upper];
if (i == jj[i])
jj_[i] = jj[i], rank[i] = 0;
else if (rank[jj[i]] == rank[jj_[jj[i]]])
jj_[i] = jj_[jj_[jj[i]]], rank[i] = rank[jj[i]] + 1;
else
jj_[i] = jj[i], rank[i] = 1;
}
while (q--) {
int k;
scanf("%d%d", &i, &j);
k = 1;
while (rr[i] < j) {
if (i == jj[i]) {
k = -1;
break;
}
if (rr[jj_[i]] < j)
k += (1 << rank[i]) - 1, i = jj_[i];
else
k++, i = jj[i];
}
printf("%d\n", k);
}
return 0;
}
Compilation message
Main.c: In function 'main':
Main.c:57:15: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
57 | for (t = s; t = ff_[t][a]; t = ff[t])
| ^
Main.c:48:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | scanf("%d%d", &m, &q);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:50:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf("%s", bb);
| ^~~~~~~~~~~~~~~
Main.c:53:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | scanf("%s", aa), n = strlen(aa);
| ^~~~~~~~~~~~~~~
Main.c:87:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | scanf("%d%d", &i, &j);
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
300 KB |
Output is correct |
2 |
Correct |
143 ms |
84628 KB |
Output is correct |
3 |
Correct |
135 ms |
61908 KB |
Output is correct |
4 |
Correct |
183 ms |
82604 KB |
Output is correct |
5 |
Correct |
149 ms |
75072 KB |
Output is correct |
6 |
Correct |
172 ms |
95796 KB |
Output is correct |
7 |
Correct |
147 ms |
106416 KB |
Output is correct |
8 |
Correct |
175 ms |
101540 KB |
Output is correct |
9 |
Correct |
157 ms |
91560 KB |
Output is correct |
10 |
Correct |
90 ms |
23888 KB |
Output is correct |
11 |
Correct |
114 ms |
97856 KB |
Output is correct |
12 |
Correct |
87 ms |
47676 KB |
Output is correct |
13 |
Correct |
104 ms |
75340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
41316 KB |
Output is correct |
2 |
Correct |
97 ms |
67020 KB |
Output is correct |
3 |
Correct |
63 ms |
45668 KB |
Output is correct |
4 |
Correct |
46 ms |
22392 KB |
Output is correct |
5 |
Correct |
47 ms |
21428 KB |
Output is correct |
6 |
Correct |
92 ms |
67844 KB |
Output is correct |
7 |
Correct |
108 ms |
78700 KB |
Output is correct |
8 |
Correct |
84 ms |
56612 KB |
Output is correct |
9 |
Correct |
94 ms |
70888 KB |
Output is correct |
10 |
Correct |
119 ms |
86604 KB |
Output is correct |
11 |
Correct |
71 ms |
46688 KB |
Output is correct |
12 |
Correct |
64 ms |
39300 KB |
Output is correct |
13 |
Correct |
106 ms |
95648 KB |
Output is correct |
14 |
Correct |
96 ms |
85036 KB |
Output is correct |
15 |
Correct |
36 ms |
12576 KB |
Output is correct |
16 |
Correct |
58 ms |
94876 KB |
Output is correct |
17 |
Correct |
33 ms |
32844 KB |
Output is correct |
18 |
Correct |
31 ms |
31876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
300 KB |
Output is correct |
2 |
Correct |
143 ms |
84628 KB |
Output is correct |
3 |
Correct |
135 ms |
61908 KB |
Output is correct |
4 |
Correct |
183 ms |
82604 KB |
Output is correct |
5 |
Correct |
149 ms |
75072 KB |
Output is correct |
6 |
Correct |
172 ms |
95796 KB |
Output is correct |
7 |
Correct |
147 ms |
106416 KB |
Output is correct |
8 |
Correct |
175 ms |
101540 KB |
Output is correct |
9 |
Correct |
157 ms |
91560 KB |
Output is correct |
10 |
Correct |
90 ms |
23888 KB |
Output is correct |
11 |
Correct |
114 ms |
97856 KB |
Output is correct |
12 |
Correct |
87 ms |
47676 KB |
Output is correct |
13 |
Correct |
104 ms |
75340 KB |
Output is correct |
14 |
Correct |
62 ms |
41316 KB |
Output is correct |
15 |
Correct |
97 ms |
67020 KB |
Output is correct |
16 |
Correct |
63 ms |
45668 KB |
Output is correct |
17 |
Correct |
46 ms |
22392 KB |
Output is correct |
18 |
Correct |
47 ms |
21428 KB |
Output is correct |
19 |
Correct |
92 ms |
67844 KB |
Output is correct |
20 |
Correct |
108 ms |
78700 KB |
Output is correct |
21 |
Correct |
84 ms |
56612 KB |
Output is correct |
22 |
Correct |
94 ms |
70888 KB |
Output is correct |
23 |
Correct |
119 ms |
86604 KB |
Output is correct |
24 |
Correct |
71 ms |
46688 KB |
Output is correct |
25 |
Correct |
64 ms |
39300 KB |
Output is correct |
26 |
Correct |
106 ms |
95648 KB |
Output is correct |
27 |
Correct |
96 ms |
85036 KB |
Output is correct |
28 |
Correct |
36 ms |
12576 KB |
Output is correct |
29 |
Correct |
58 ms |
94876 KB |
Output is correct |
30 |
Correct |
33 ms |
32844 KB |
Output is correct |
31 |
Correct |
31 ms |
31876 KB |
Output is correct |
32 |
Correct |
128 ms |
19856 KB |
Output is correct |
33 |
Correct |
198 ms |
64228 KB |
Output is correct |
34 |
Correct |
203 ms |
88260 KB |
Output is correct |
35 |
Correct |
181 ms |
77132 KB |
Output is correct |
36 |
Correct |
205 ms |
102192 KB |
Output is correct |
37 |
Correct |
138 ms |
18380 KB |
Output is correct |
38 |
Correct |
279 ms |
95388 KB |
Output is correct |
39 |
Correct |
243 ms |
48404 KB |
Output is correct |
40 |
Correct |
300 ms |
109180 KB |
Output is correct |
41 |
Correct |
285 ms |
112584 KB |
Output is correct |
42 |
Correct |
240 ms |
70968 KB |
Output is correct |
43 |
Correct |
196 ms |
63872 KB |
Output is correct |
44 |
Correct |
193 ms |
59544 KB |
Output is correct |
45 |
Correct |
184 ms |
56780 KB |
Output is correct |
46 |
Correct |
285 ms |
93900 KB |
Output is correct |
47 |
Correct |
212 ms |
25180 KB |
Output is correct |
48 |
Correct |
233 ms |
55280 KB |
Output is correct |
49 |
Correct |
206 ms |
28792 KB |
Output is correct |
50 |
Correct |
262 ms |
102340 KB |
Output is correct |
51 |
Correct |
111 ms |
19020 KB |
Output is correct |
52 |
Correct |
103 ms |
41764 KB |
Output is correct |
53 |
Correct |
107 ms |
21588 KB |
Output is correct |