#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define oo 1e9
const int MAX = 1e4 + 5, MAX2 = 500 + 5, B = 61, MOD = 1e9 + 7;
int n, q;
string s;
string t[MAX];
map<int, int> mp;
int pre[MAX2];
int dp[MAX2][MAX2];
int P[(int)5e5 + 5];
int invP[(int)5e5 + 5];
int binpow(int a, int b){
if(b == 0) return 1;
if(b & 1) return binpow(a, b - 1) % MOD * a % MOD;
return binpow(a * a % MOD, b / 2) % MOD;
}
int inv(int a){
return binpow(a, MOD - 2);
}
int calc(int l, int r){
return (pre[r] - pre[l - 1] + 2 * MOD) % MOD * invP[l - 1] % MOD;
}
void solve(){
P[0] = 1;
invP[0] = 1;
for(int i = 1; i <= 5e5; i++){
P[i] = P[i - 1] * B % MOD;
invP[i] = invP[i - 1] * inv(B) % MOD;
}
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> t[i];
int H = 0;
for(int j = 0; j < t[i].size(); j++){
H = (H + (t[i][j] - 'a' + 1) * P[j] % MOD) % MOD;
mp[H] = 1;
}
cout << '\n';
}
cin >> s;
int L = s.size();
for(int i = 1; i <= L; i++){
pre[i] = (pre[i - 1] + (s[i - 1] - 'a' + 1) * P[i - 1] % MOD) % MOD;
}
for(int sz = 1; sz <= L; sz++){
for(int i = 1; i + sz - 1 <= L; i++){
int j = i + sz - 1;
dp[i][j] = oo;
if(mp.count(calc(i, j))){
dp[i][j] = 1;
continue;
}
for(int k = i; k <= j - 1; k++){
dp[i][j] = min(dp[i][k] + dp[k + 1][j], dp[i][j]);
}
}
}
while(q--){
int l, r; cin >> l >> r;
l++;
if(dp[l][r] == oo) dp[l][r] = -1;
cout << dp[l][r] << '\n';
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while(t--){
solve();
}
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:45:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int j = 0; j < t[i].size(); j++){
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
156 ms |
8532 KB |
Output is correct |
2 |
Incorrect |
322 ms |
35668 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
210 ms |
39484 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
156 ms |
8532 KB |
Output is correct |
2 |
Incorrect |
322 ms |
35668 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |