#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e9 + 100;
const ll LINF = 1e18 + 100;
const int prm = 727;
#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define fast_io ios::sync_with_stdio(0); cin.tie(0);
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fr first
#define sc second
#define endl '\n'
const int MAXN = 300100;
const int LOGN = 22;
int n, q;
char s[MAXN];
unordered_set<int> st;
int pw[MAXN];
int hsh[MAXN];
int dp[LOGN][MAXN];
inline int gethash(int l, int r)
{
return (mod + (hsh[r + 1] - hsh[l] * (ll)pw[r - l + 1]) % mod) % mod;
}
struct SEG
{
int seg[2 * MAXN];
inline void build(int *dp)
{
FOR(i, 0, MAXN) seg[i + MAXN] = dp[i];
for (int i = MAXN - 1; i > 0; i--) seg[i] = max(seg[2 * i], seg[2 * i + 1]);
}
inline int get(int l, int r)
{
r++;
int res = -INF;
for (l += MAXN, r += MAXN; l < r; l /= 2, r /= 2)
{
if (l % 2) res = max(res, seg[l++]);
if (r % 2) res = max(res, seg[--r]);
}
return res;
}
} seg[LOGN];
int32_t main(void)
{
fast_io;
int tmp;
cin >> tmp >> q;
while (tmp--)
{
string t;
cin >> t;
int h = 0;
for (char c : t)
{
h = (h * (ll)prm + c) % mod;
st.insert(h);
}
}
cin >> s;
n = strlen(s);
pw[0] = 1;
FOR(i, 1, MAXN) pw[i] = pw[i - 1] * (ll)prm % mod;
FOR(i, 1, n + 1) hsh[i] = (hsh[i - 1] * (ll)prm + s[i - 1]) % mod;
FOR(i, 0, n)
{
int l = 0, r = n - i + 1;
while (r - l > 1)
{
int mid = (r + l) / 2;
if (st.count(gethash(i, i + mid - 1))) l = mid;
else r = mid;
}
dp[0][i] = i + l - 1;
}
FOR(i, 1, LOGN)
{
seg[i - 1].build(dp[i - 1]);
FOR(j, 0, n)
{
dp[i][j] = seg[i - 1].get(j, dp[i - 1][j] + 1);
}
}
seg[LOGN - 1].build(dp[LOGN - 1]);
while (q--)
{
int l, r;
cin >> l >> r;
r--;
int ptr = l;
int cost = 0;
for (int i = LOGN - 1; i >= 0; i--)
{
int fw = seg[i].get(l, ptr);
if (fw >= r) continue;
cost += (1 << i);
ptr = fw + 1;
}
if (cost > n) cout << "-1\n";
else cout << cost + 1 << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
53188 KB |
Output is correct |
2 |
Correct |
298 ms |
71604 KB |
Output is correct |
3 |
Correct |
299 ms |
65536 KB |
Output is correct |
4 |
Incorrect |
365 ms |
71100 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
881 ms |
87084 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
53188 KB |
Output is correct |
2 |
Correct |
298 ms |
71604 KB |
Output is correct |
3 |
Correct |
299 ms |
65536 KB |
Output is correct |
4 |
Incorrect |
365 ms |
71100 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |