#include <stdio.h>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
const int maxn = 2000002;
map<char, int> trie[2][maxn];
vector<int> ends[2][maxn]; int cnt[2];
int L[2][maxn],R[2][maxn],A[2][maxn],I[maxn],C[2];
const int Z = 1<<20;
vector<int> IT[Z*2];
int N,M; char S[maxn];
void dfs(int k, int n)
{
L[k][n] = C[k];
for (auto &x : ends[k][n]) A[k][C[k]++] = x;
for (auto &p : trie[k][n]) dfs(k,p.second);
R[k][n] = C[k]-1;
}
int get(int i, int l, int r)
{
return upper_bound(IT[i].begin(),IT[i].end(),r) - lower_bound(IT[i].begin(),IT[i].end(),l);
}
int query(int l, int r, int x, int y)
{
int res = 0;
x += Z; y += Z;
while (x < y){
if (x & 1) res += get(x++,l,r);
if (~y & 1) res += get(y--,l,r);
x /= 2; y /= 2;
} if (x == y) res += get(x,l,r);
return res;
}
int main()
{
//freopen ("input.txt","r",stdin);
scanf ("%d %d",&N,&M);
for (int i=0;i<N;i++){
scanf ("%s",S);
int L = 0;
while (S[L]) L++;
for (int k=0;k<2;k++){
int n = 0;
for (int j=0;S[j];j++){
int &nxt = trie[k][n][S[j]];
if (!nxt) nxt = ++cnt[k];
n = nxt;
}
ends[k][n].push_back(i);
if (!k) reverse(S,S+L);
}
}
for (int k=0;k<2;k++) dfs(k,0);
for (int i=0;i<N;i++) I[A[0][i]] = i;
for (int i=0;i<N;i++) A[1][i] = I[A[1][i]];
for (int i=0;i<N;i++) I[A[1][i]] = i;
for (int i=0;i<N;i++){
int x = I[i] + Z;
while (x){
IT[x].push_back(i);
x /= 2;
}
}
for (int i=0;i<M;i++){
int n[2] = {0,};
for (int k=0;k<2;k++){
scanf ("%s",S);
if (k){
int l = 0;
while (S[l]) l++;
reverse(S,S+l);
}
for (int j=0;S[j];j++){
if (trie[k][n[k]].count(S[j])) n[k] = trie[k][n[k]][S[j]];
else{n[k] = -1; break;}
}
}
int ans;
if (n[0] == -1 || n[1] == -1) ans = 0;
else ans = query(L[0][n[0]],R[0][n[0]],L[1][n[1]],R[1][n[1]]);
printf ("%d\n",ans);
}
return 0;
}
Compilation message
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:47:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d %d",&N,&M);
^
selling_rna.cpp:49:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%s",S);
^
selling_rna.cpp:79:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%s",S);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
289 ms |
331556 KB |
Output is correct |
2 |
Correct |
278 ms |
331584 KB |
Output is correct |
3 |
Correct |
275 ms |
331692 KB |
Output is correct |
4 |
Correct |
272 ms |
331792 KB |
Output is correct |
5 |
Correct |
272 ms |
331864 KB |
Output is correct |
6 |
Correct |
274 ms |
331864 KB |
Output is correct |
7 |
Correct |
281 ms |
331864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
617 ms |
444592 KB |
Output is correct |
2 |
Correct |
610 ms |
444592 KB |
Output is correct |
3 |
Correct |
631 ms |
450880 KB |
Output is correct |
4 |
Correct |
616 ms |
450880 KB |
Output is correct |
5 |
Correct |
705 ms |
484516 KB |
Output is correct |
6 |
Correct |
688 ms |
488976 KB |
Output is correct |
7 |
Correct |
379 ms |
488976 KB |
Output is correct |
8 |
Correct |
616 ms |
488976 KB |
Output is correct |
9 |
Correct |
662 ms |
488976 KB |
Output is correct |
10 |
Correct |
514 ms |
488976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
345 ms |
488976 KB |
Output is correct |
2 |
Correct |
337 ms |
488976 KB |
Output is correct |
3 |
Correct |
357 ms |
488976 KB |
Output is correct |
4 |
Correct |
324 ms |
488976 KB |
Output is correct |
5 |
Correct |
343 ms |
488976 KB |
Output is correct |
6 |
Correct |
347 ms |
488976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
289 ms |
331556 KB |
Output is correct |
2 |
Correct |
278 ms |
331584 KB |
Output is correct |
3 |
Correct |
275 ms |
331692 KB |
Output is correct |
4 |
Correct |
272 ms |
331792 KB |
Output is correct |
5 |
Correct |
272 ms |
331864 KB |
Output is correct |
6 |
Correct |
274 ms |
331864 KB |
Output is correct |
7 |
Correct |
281 ms |
331864 KB |
Output is correct |
8 |
Correct |
617 ms |
444592 KB |
Output is correct |
9 |
Correct |
610 ms |
444592 KB |
Output is correct |
10 |
Correct |
631 ms |
450880 KB |
Output is correct |
11 |
Correct |
616 ms |
450880 KB |
Output is correct |
12 |
Correct |
705 ms |
484516 KB |
Output is correct |
13 |
Correct |
688 ms |
488976 KB |
Output is correct |
14 |
Correct |
379 ms |
488976 KB |
Output is correct |
15 |
Correct |
616 ms |
488976 KB |
Output is correct |
16 |
Correct |
662 ms |
488976 KB |
Output is correct |
17 |
Correct |
514 ms |
488976 KB |
Output is correct |
18 |
Correct |
345 ms |
488976 KB |
Output is correct |
19 |
Correct |
337 ms |
488976 KB |
Output is correct |
20 |
Correct |
357 ms |
488976 KB |
Output is correct |
21 |
Correct |
324 ms |
488976 KB |
Output is correct |
22 |
Correct |
343 ms |
488976 KB |
Output is correct |
23 |
Correct |
347 ms |
488976 KB |
Output is correct |
24 |
Correct |
619 ms |
488976 KB |
Output is correct |
25 |
Correct |
671 ms |
488976 KB |
Output is correct |
26 |
Correct |
638 ms |
488976 KB |
Output is correct |
27 |
Correct |
651 ms |
488976 KB |
Output is correct |
28 |
Correct |
719 ms |
488976 KB |
Output is correct |
29 |
Correct |
484 ms |
488976 KB |
Output is correct |