#include <bits/stdc++.h>
using namespace std;
const int mxN1=1e5, mxN2=2e6;
int n, m, enc[256], ans[mxN1], ft[mxN2+1];
string ss[mxN1];
vector<int> pts[mxN2];
struct query {
int i, l, r, w;
};
vector<query> queries[mxN2+1];
struct trie {
int sz=1, c[mxN2][4], dt, st[mxN2], en[mxN2];
inline void ins(string &s) {
for(int i=0, u=0; i<s.size(); ++i) {
if(!c[u][enc[s[i]]])
c[u][enc[s[i]]]=sz++;
u=c[u][enc[s[i]]];
}
}
inline void dfs1(int u=0) {
st[u]=dt++;
for(int i=0; i<4; ++i)
if(c[u][i])
dfs1(c[u][i]);
en[u]=dt;
}
inline int gi(string &s) {
int u=0;
for(int i=0; i<s.size(); u=c[u][enc[s[i]]], ++i)
if(!c[u][enc[s[i]]])
return -1;
return u;
}
} t[2];
inline void upd(int i, int x) {
for(++i; i<=mxN2; i+=i&-i)
ft[i]+=x;
}
inline int qry(int i) {
int r=0;
for(; i; i-=i&-i)
r+=ft[i];
return r;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
enc['A']=0;
enc['G']=1;
enc['C']=2;
enc['U']=3;
cin >> n >> m;
for(int i=0; i<n; ++i) {
cin >> ss[i];
t[0].ins(ss[i]);
reverse(ss[i].begin(), ss[i].end());
t[1].ins(ss[i]);
}
t[0].dfs1();
t[1].dfs1();
for(int i=0; i<n; ++i) {
int y=t[1].st[t[1].gi(ss[i])];
reverse(ss[i].begin(), ss[i].end());
int x=t[0].st[t[0].gi(ss[i])];
pts[y].push_back(x);
}
for(int i=0; i<m; ++i) {
string p, q;
cin >> p >> q, reverse(q.begin(), q.end());
int pi=t[0].gi(p), qi=t[1].gi(q);
if(pi==-1||qi==-1)
continue;
queries[t[1].st[qi]].push_back({i, t[0].st[pi], t[0].en[pi], -1});
queries[t[1].en[qi]].push_back({i, t[0].st[pi], t[0].en[pi], 1});
}
for(int i=0; i<mxN2; ++i) {
for(int x : pts[i])
upd(x, 1);
for(query q : queries[i+1])
ans[q.i]+=q.w*(qry(q.r)-qry(q.l));
}
for(int i=0; i<m; ++i)
cout << ans[i] << "\n";
}
Compilation message
selling_rna.cpp: In member function 'void trie::ins(std::__cxx11::string&)':
selling_rna.cpp:17:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0, u=0; i<s.size(); ++i) {
^
selling_rna.cpp:18:21: warning: array subscript has type 'char' [-Wchar-subscripts]
if(!c[u][enc[s[i]]])
^
selling_rna.cpp:19:18: warning: array subscript has type 'char' [-Wchar-subscripts]
c[u][enc[s[i]]]=sz++;
^
selling_rna.cpp:20:19: warning: array subscript has type 'char' [-Wchar-subscripts]
u=c[u][enc[s[i]]];
^
selling_rna.cpp: In member function 'int trie::gi(std::__cxx11::string&)':
selling_rna.cpp:32:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<s.size(); u=c[u][enc[s[i]]], ++i)
^
selling_rna.cpp:32:43: warning: array subscript has type 'char' [-Wchar-subscripts]
for(int i=0; i<s.size(); u=c[u][enc[s[i]]], ++i)
^
selling_rna.cpp:33:21: warning: array subscript has type 'char' [-Wchar-subscripts]
if(!c[u][enc[s[i]]])
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
97528 KB |
Output is correct |
2 |
Correct |
96 ms |
97740 KB |
Output is correct |
3 |
Correct |
95 ms |
97740 KB |
Output is correct |
4 |
Correct |
98 ms |
97824 KB |
Output is correct |
5 |
Correct |
97 ms |
97824 KB |
Output is correct |
6 |
Correct |
109 ms |
97824 KB |
Output is correct |
7 |
Correct |
97 ms |
97824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
251 ms |
150588 KB |
Output is correct |
2 |
Correct |
252 ms |
151984 KB |
Output is correct |
3 |
Correct |
251 ms |
165192 KB |
Output is correct |
4 |
Correct |
254 ms |
166716 KB |
Output is correct |
5 |
Correct |
295 ms |
179748 KB |
Output is correct |
6 |
Correct |
302 ms |
183128 KB |
Output is correct |
7 |
Correct |
142 ms |
183128 KB |
Output is correct |
8 |
Correct |
233 ms |
183128 KB |
Output is correct |
9 |
Correct |
220 ms |
183128 KB |
Output is correct |
10 |
Correct |
195 ms |
183128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
121 ms |
183128 KB |
Output is correct |
2 |
Correct |
116 ms |
183128 KB |
Output is correct |
3 |
Correct |
122 ms |
183128 KB |
Output is correct |
4 |
Correct |
116 ms |
183128 KB |
Output is correct |
5 |
Correct |
121 ms |
183128 KB |
Output is correct |
6 |
Correct |
122 ms |
183128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
97528 KB |
Output is correct |
2 |
Correct |
96 ms |
97740 KB |
Output is correct |
3 |
Correct |
95 ms |
97740 KB |
Output is correct |
4 |
Correct |
98 ms |
97824 KB |
Output is correct |
5 |
Correct |
97 ms |
97824 KB |
Output is correct |
6 |
Correct |
109 ms |
97824 KB |
Output is correct |
7 |
Correct |
97 ms |
97824 KB |
Output is correct |
8 |
Correct |
251 ms |
150588 KB |
Output is correct |
9 |
Correct |
252 ms |
151984 KB |
Output is correct |
10 |
Correct |
251 ms |
165192 KB |
Output is correct |
11 |
Correct |
254 ms |
166716 KB |
Output is correct |
12 |
Correct |
295 ms |
179748 KB |
Output is correct |
13 |
Correct |
302 ms |
183128 KB |
Output is correct |
14 |
Correct |
142 ms |
183128 KB |
Output is correct |
15 |
Correct |
233 ms |
183128 KB |
Output is correct |
16 |
Correct |
220 ms |
183128 KB |
Output is correct |
17 |
Correct |
195 ms |
183128 KB |
Output is correct |
18 |
Correct |
121 ms |
183128 KB |
Output is correct |
19 |
Correct |
116 ms |
183128 KB |
Output is correct |
20 |
Correct |
122 ms |
183128 KB |
Output is correct |
21 |
Correct |
116 ms |
183128 KB |
Output is correct |
22 |
Correct |
121 ms |
183128 KB |
Output is correct |
23 |
Correct |
122 ms |
183128 KB |
Output is correct |
24 |
Correct |
251 ms |
186512 KB |
Output is correct |
25 |
Correct |
268 ms |
192340 KB |
Output is correct |
26 |
Correct |
257 ms |
193776 KB |
Output is correct |
27 |
Correct |
276 ms |
204548 KB |
Output is correct |
28 |
Correct |
274 ms |
204548 KB |
Output is correct |
29 |
Correct |
155 ms |
204548 KB |
Output is correct |