#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 = 2e3 + 2;
const int INF = 1e9 + 7;
int n, q;
string s[N];
int id[N];
int pre[M][4];
pair<int,int> range[N];
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
8 ms |
15708 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
5980 KB |
Output is correct |
2 |
Runtime error |
9 ms |
10332 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Runtime error |
8 ms |
15708 KB |
Execution killed with signal 11 |
9 |
Halted |
0 ms |
0 KB |
- |