#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define x first
#define y second
#define all(x) x.begin(), x.end()
const int N = 2e6+5;
struct coorh {
size_t operator()(const pii &a) const {
return a.x + a.y;
}
};
int n, m;
pii add(pii a, pii b) { return pii(a.x + b.x, a.y + b.y); }
pii mul(pii a, pii b) { return pii(a.x * b.x, a.y * b.y); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
vector<string> str;
for(int i = 0; i < n; ++i) {
string now; cin >> now;
str.emplace_back(now);
}
sort(all(str));
unordered_map<pii, vector<int>, coorh> hr;
unordered_map<pii, pii, coorh> hl;
for(int i = 0; i < n; ++i) {
int len = str[i].size();
pii hashl, hashr;
for(int j = 0; j < len; ++j) {
hashl = add(mul(hashl, pii(131, 133)), pii(str[i][j], str[i][j]));
if(hl.count(hashl)) hl[hashl].y = i;
else hl[hashl] = pii(i, i);
}
for(int j = len-1; ~j; --j)
hashr = add(mul(hashr, pii(131, 133)), pii(str[i][j], str[i][j])), hr[hashr].emplace_back(i);
}
for(int i = 0; i < m; ++i) {
string a, b; cin >> a >> b;
pii hashl, hashr;
for(int j = 0; j < a.size(); ++j)
hashl = add(mul(hashl, pii(131, 133)), pii(a[j], a[j]));
for(int j = b.size()-1; ~j; --j)
hashr = add(mul(hashr, pii(131, 133)), pii(b[j], b[j]));
if(!hl.count(hashl)) {
puts("0");
continue;
}
int l = hl[hashl].x, r = hl[hashl].y;
int dis = upper_bound(all(hr[hashr]), r) - lower_bound(all(hr[hashr]), l);
printf("%d\n", dis);
}
}
Compilation message
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:48:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < a.size(); ++j)
~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
500 KB |
Output is correct |
3 |
Correct |
2 ms |
500 KB |
Output is correct |
4 |
Correct |
2 ms |
532 KB |
Output is correct |
5 |
Correct |
2 ms |
572 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1600 ms |
218840 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
218840 KB |
Output is correct |
2 |
Correct |
40 ms |
218840 KB |
Output is correct |
3 |
Correct |
49 ms |
218840 KB |
Output is correct |
4 |
Correct |
38 ms |
218840 KB |
Output is correct |
5 |
Correct |
42 ms |
218840 KB |
Output is correct |
6 |
Correct |
50 ms |
218840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
500 KB |
Output is correct |
3 |
Correct |
2 ms |
500 KB |
Output is correct |
4 |
Correct |
2 ms |
532 KB |
Output is correct |
5 |
Correct |
2 ms |
572 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
608 KB |
Output is correct |
8 |
Execution timed out |
1600 ms |
218840 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |