#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
#define SZ(x) (int) x.size()
#define F first
#define S second
#define lc id << 1
#define rc lc | 1
const int N = 500 + 10, MOD = 1e9 + 9;
int pw[N], H[N], dp[N], nxt[N], n, m, q, base = 31; unordered_map<int, int> mp;
char S[N * 2]; int seg[N << 2];
int get_hash(int l, int r) {
return (H[r] - 1ll * H[l - 1] * pw[r - l + 1] % MOD + MOD) % MOD;
}
void upd(int p, int x, int id = 1, int l = 1, int r = n + 2) {
if (r - l < 2) { seg[id] = x; return; }
int md = (l + r) >> 1;
p < md ? upd(p, x, lc, l, md) : upd(p, x, rc, md, r);
seg[id] = min(seg[lc], seg[rc]);
}
int get(int ql, int qr, int id = 1, int l = 1, int r = n + 2) {
if (qr <= l || r <= ql) return MOD;
if (ql <= l && r <= qr) return seg[id];
int md = (l + r) >> 1;
return min(get(ql, qr, lc, l, md), get(ql, qr, rc, md, r));
}
int main() {
pw[0] = 1;
for (int i = 1; i < N; i++)
pw[i] = 1ll * pw[i - 1] * base % MOD;
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++) {
scanf("%s", S);
int h = 0;
for (int j = 0; S[j]; j++) {
h = (1ll * h * base % MOD + S[j] - 'a' + 1) % MOD;
mp[h] = 1;
}
}
scanf("%s", S + 1); m = strlen(S + 1);
for (int i = 1; i <= m; i++) {
H[i] = (1ll * H[i - 1] * base % MOD + S[i] - 'a' + 1) % MOD;
}
swap(n, m);
for (int i = 1; i <= n; i++) {
int l = i, r = n + 1;
while (r - l > 1) {
int md = (l + r) >> 1;
if (mp.count(get_hash(i, md))) l = md;
else r = md;
}
nxt[i] = (mp.count(get_hash(i, i)) ? l : -MOD);
}
for (; q; q--) {
int l, r; scanf("%d%d", &l, &r); l++;
memset(dp, 0, sizeof dp); fill(seg, seg + N * 4, MOD);
upd(r + 1, 0);
for (int i = r; i >= l; i--) {
if (nxt[i] < 0) dp[i] = MOD;
else dp[i] = get(i + 1, nxt[i] + 2) + 1;
upd(i, dp[i]);
}
printf("%d\n", dp[l] > n ? -1 : dp[l]);
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:40:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d%d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:42:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | scanf("%s", S);
| ~~~~~^~~~~~~~~
Main.cpp:49:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | scanf("%s", S + 1); m = strlen(S + 1);
| ~~~~~^~~~~~~~~~~~~
Main.cpp:64:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | int l, r; scanf("%d%d", &l, &r); l++;
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1831 ms |
19656 KB |
Output is correct |
3 |
Execution timed out |
2067 ms |
11940 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
37 ms |
13632 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1831 ms |
19656 KB |
Output is correct |
3 |
Execution timed out |
2067 ms |
11940 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |