#include <bits/stdc++.h>
using namespace std;
#define uwu "RNA"
typedef long long ll;
#define pb push_back
#define endl '\n'
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORD(i, a, b) for(int i = a; i >= b; i--)
const int N = 1e5 + 3;
const int M = 2e6 + 2;
const int INF = 1e9 + 7;
int n, q;
string s[N];
int id[N];
int pre[M][4];
pair<int,int> range[M];
int nodepre = 1;
int suf[M][4];
vector <int> vt[M];
int nodesuf = 1;
int num(char c){
if (c == 'U') return 3;
if (c == 'G') return 1;
return c - 'A';
}
void addpre(string s, int inc){
int pos = 1, len = s.size();
FOR(i, 0, len - 1){
int c = num(s[i]);
if (!pre[pos][c]){
pre[pos][c] = ++nodepre;
range[nodepre].first = INF;
}
pos = pre[pos][c];
range[pos].first = min(range[pos].first, inc);
range[pos].second = max(range[pos].second, inc);
}
}
void addsuf(string s, int inc){
int pos = 1, len = s.size();
FORD(i, len - 1, 0){
int c = num(s[i]);
if (!suf[pos][c]) suf[pos][c] = ++nodesuf;
pos = suf[pos][c]; vt[pos].pb(inc);
}
}
pair<int,int> findpre(string s){
int pos = 1, len = s.size();
FOR(i, 0, len - 1){
int c = num(s[i]); pos = pre[pos][c];
if (pos == 0) break;
}
return range[pos];
}
int calc(string s, pair<int,int> inc){
int pos = 1, len = s.size();
FORD(i, len - 1, 0){
int c = num(s[i]); pos = suf[pos][c];
if (pos == 0) break;
}
int L = lower_bound(vt[pos].begin(), vt[pos].end(), inc.first) - vt[pos].begin();
int R = upper_bound(vt[pos].begin(), vt[pos].end(), inc.second) - vt[pos].begin();
return R - L;
}
void solve(void){
FOR(i, 1, n) id[i] = i;
sort(id + 1, id + n + 1, [&] (int u, int v){
return s[u] < s[v];
});
FOR(i, 1, n){
addpre(s[id[i]], i);
addsuf(s[id[i]], i);
}
while(q--){
string P, S; cin >> P >> S;
cout << calc(S, findpre(P)) << endl;
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if(fopen(uwu".inp","r")){
freopen(uwu".inp","r",stdin);
freopen(uwu".out","w",stdout);
}
cin >> n >> q;
FOR(i, 1, n) cin >> s[i];
solve();
cerr << "\nTime used: " << clock() << "ms\n";
return 0;
}
Compilation message
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:93:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | freopen(uwu".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:94:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | freopen(uwu".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
50780 KB |
Output is correct |
2 |
Correct |
14 ms |
50780 KB |
Output is correct |
3 |
Correct |
14 ms |
50780 KB |
Output is correct |
4 |
Correct |
18 ms |
50644 KB |
Output is correct |
5 |
Correct |
14 ms |
50776 KB |
Output is correct |
6 |
Correct |
18 ms |
50788 KB |
Output is correct |
7 |
Correct |
17 ms |
50792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
127 ms |
145464 KB |
Output is correct |
2 |
Correct |
127 ms |
140372 KB |
Output is correct |
3 |
Correct |
72 ms |
109656 KB |
Output is correct |
4 |
Correct |
57 ms |
107192 KB |
Output is correct |
5 |
Correct |
86 ms |
137812 KB |
Output is correct |
6 |
Correct |
89 ms |
139092 KB |
Output is correct |
7 |
Correct |
38 ms |
61036 KB |
Output is correct |
8 |
Correct |
108 ms |
108904 KB |
Output is correct |
9 |
Correct |
94 ms |
100528 KB |
Output is correct |
10 |
Correct |
96 ms |
99840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
51804 KB |
Output is correct |
2 |
Correct |
27 ms |
51992 KB |
Output is correct |
3 |
Correct |
28 ms |
52056 KB |
Output is correct |
4 |
Correct |
26 ms |
52060 KB |
Output is correct |
5 |
Correct |
29 ms |
52080 KB |
Output is correct |
6 |
Correct |
27 ms |
52104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
50780 KB |
Output is correct |
2 |
Correct |
14 ms |
50780 KB |
Output is correct |
3 |
Correct |
14 ms |
50780 KB |
Output is correct |
4 |
Correct |
18 ms |
50644 KB |
Output is correct |
5 |
Correct |
14 ms |
50776 KB |
Output is correct |
6 |
Correct |
18 ms |
50788 KB |
Output is correct |
7 |
Correct |
17 ms |
50792 KB |
Output is correct |
8 |
Correct |
127 ms |
145464 KB |
Output is correct |
9 |
Correct |
127 ms |
140372 KB |
Output is correct |
10 |
Correct |
72 ms |
109656 KB |
Output is correct |
11 |
Correct |
57 ms |
107192 KB |
Output is correct |
12 |
Correct |
86 ms |
137812 KB |
Output is correct |
13 |
Correct |
89 ms |
139092 KB |
Output is correct |
14 |
Correct |
38 ms |
61036 KB |
Output is correct |
15 |
Correct |
108 ms |
108904 KB |
Output is correct |
16 |
Correct |
94 ms |
100528 KB |
Output is correct |
17 |
Correct |
96 ms |
99840 KB |
Output is correct |
18 |
Correct |
31 ms |
51804 KB |
Output is correct |
19 |
Correct |
27 ms |
51992 KB |
Output is correct |
20 |
Correct |
28 ms |
52056 KB |
Output is correct |
21 |
Correct |
26 ms |
52060 KB |
Output is correct |
22 |
Correct |
29 ms |
52080 KB |
Output is correct |
23 |
Correct |
27 ms |
52104 KB |
Output is correct |
24 |
Correct |
106 ms |
132628 KB |
Output is correct |
25 |
Correct |
135 ms |
133008 KB |
Output is correct |
26 |
Correct |
123 ms |
131680 KB |
Output is correct |
27 |
Correct |
69 ms |
104552 KB |
Output is correct |
28 |
Correct |
118 ms |
76448 KB |
Output is correct |
29 |
Correct |
74 ms |
59532 KB |
Output is correct |