#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const int sq = 400;
const ll A = 976186659, B = 988144425;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
vector<vector<ll>> pref, suff;
unordered_map<ll, int> mp;
for(int i = 0; i < n; i++){
str s; cin >> s;
if(s.size() < sq){
ll h = 0;
for(int j = 0; j < s.size(); j++){
h*=A, h%=B;
h+=s[j], h%=B;
ll hh = 0, p = 1;
for(int k = int(s.size())-1; k >= 0; k--){
hh+=(s[k]*p)%B, hh%=B;
p*=A, p%=B;
mp[h*ll(1e9)+hh]++;
}
}
}
else{
pref.pb(vector<ll>(s.size())), suff.pb(vector<ll>(s.size()));
ll h = 0;
for(int j = 0; j < s.size(); j++){
h*=A, h%=B;
h+=s[j], h%=B;
pref.back()[j] = h;
}
h = 0; ll p = 1;
for(int j = int(s.size())-1; j >= 0; j--){
h+=(s[j]*p)%B, h%=B;
p*=A, p%=B;
suff.back()[j] = h;
}
}
}
while(m--){
str p, q; cin >> p >> q;
ll h1 = 0, h2 = 0;
for(int i = 0; i < p.size(); i++) h1*=A, h1%=B, h1+=p[i], h1%=B;
for(int i = 0; i < q.size(); i++) h2*=A, h2%=B, h2+=q[i], h2%=B;
ll ans = mp[h1*ll(1e9)+h2];
int psz = p.size(), qsz = q.size();
for(int i = 0; i < pref.size(); i++){
int sz = pref[i].size();
if(max(psz, qsz) <= sz && pref[i][psz-1] == h1 && suff[i][sz-qsz] == h2) ans++;
}
cout << ans << "\n";
}
}
Compilation message
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:26:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for(int j = 0; j < s.size(); j++){
| ~~^~~~~~~~~~
selling_rna.cpp:40:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int j = 0; j < s.size(); j++){
| ~~^~~~~~~~~~
selling_rna.cpp:57:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int i = 0; i < p.size(); i++) h1*=A, h1%=B, h1+=p[i], h1%=B;
| ~~^~~~~~~~~~
selling_rna.cpp:58:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int i = 0; i < q.size(); i++) h2*=A, h2%=B, h2+=q[i], h2%=B;
| ~~^~~~~~~~~~
selling_rna.cpp:64:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(int i = 0; i < pref.size(); i++){
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1572 ms |
179384 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
552 KB |
Output is correct |
2 |
Correct |
66 ms |
2828 KB |
Output is correct |
3 |
Correct |
47 ms |
1544 KB |
Output is correct |
4 |
Correct |
20 ms |
468 KB |
Output is correct |
5 |
Correct |
52 ms |
2716 KB |
Output is correct |
6 |
Correct |
42 ms |
1596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Execution timed out |
1572 ms |
179384 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |