#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define endl '\n'
//data structures
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef pair<long long, long long> pll;
typedef pair<char, int> ci;
typedef pair<string, int> si;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<int>> vvi;
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define sz(a) ((int)a.size())
#define fi first
#define se second
#define whole(v) v.begin(), v.end()
#define rwhole(v) v.rbegin(), v.rend()
#define fro front
#define pqueue priority_queue
#define ubound upper_bound
#define lbound lower_bound
#define beg(v) v.begin()
//bit operations
int flip(int x){
return ~(x) ^ (1 << 32);
}
int allon(int x){
return (1LL << x) - 1;
}
bool bit(ll a, ll i){
return (1LL << i) & a;
}
#define llpc(x) __builtin_popcountll(x)
#define ipc(x) __builtin_popcount(x)
#define iclz(x) __builtin_clz(x)
#define llclz(x) __builtin_clzll(x)
#define ictz(x) __builtin_ctz(x)
#define llctz(x) __builtin_ctzll(x)
//answers
#define cYES cout << "YES" << endl
#define cYes cout << "Yes" << endl
#define cyes cout << "yes" << endl
#define cNO cout << "NO" << endl
#define cNo cout << "No" << endl
#define cno cout << "no" << endl
#define ipsb cout << -1 << endl
const ll mod2 = 998244353;
const ll mod = 1000000007;
const int inf = int(1e9); // ll inf = ll(1e18);
// read arr vec matr etc
#define fill(x, y) memset(x, y, sizeof(x))
void read(vector<int> &x){
for(auto &e:x) cin >> e;
}
void sread(vector<string> &x){
for(auto &e:x) cin >> e;
}
void mread(vector<vector<int>> &p, int nnn, int mmm){
for(int i = 0; i < nnn; ++i){
vector<int> pp;
for(int j = 0; j < mmm; ++j){
int wq; cin >> wq; pp.pb(wq);
}
p.pb(pp);
}
}
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //high quality random number generator using time as seed
//int random(int l, int r){return uniform_int_distribution<int>(l,r)(rng);} //returns a randomb number between [l, r]
// Solution
struct node{
node *child[4];
vi words;
node(){
child[0] = nullptr;
child[1] = nullptr;
child[2] = nullptr;
child[3] = nullptr;
}
};
map<char, int> mp;
void add(node *no, string s, int idx, int i){
no->words.pb(idx);
if(i == sz(s)) return;
int next = mp[s[i]];
if(no->child[next] == nullptr){
node *nextno = new node();
no->child[next] = nextno;
}
add(no->child[next], s, idx, i+1);
}
ii range(node *no, string s, int i){
if(i == sz(s)){
if (no->words.empty()) return {-1, -1};
int l = no -> words[0];
int r = no->words[sz(no->words)-1];
return {l, r};
}
int next = mp[s[i]];
if(no->child[next] == nullptr){
return {-1, -1};
}
return range(no->child[next], s, i+1);
}
int query(node *no, string s, int i, int l, int r){
if(i == sz(s)){
int ans = ubound(whole(no->words), r) - no->words.begin();
ans -= lbound(whole(no->words), l) - no->words.begin();
return ans;
}
int next = mp[s[i]];
if(no->child[next] == nullptr){
return 0;
}
return query(no->child[next], s, i+1, l, r);
}
void tc(){
int n, q; cin >> n >> q;
node *trie = new node();
node *rtrie = new node();
vs x(n); sread(x);
sort(whole(x));
for(int i = 0; i < n; ++i){
add(trie, x[i], i, 0);
reverse(whole(x[i]));
add(rtrie, x[i], i, 0);
}
while(q--){
string a, b; cin >> a >> b;
ii lr = range(trie, a, 0);
if (lr.fi == -1) {
cout << 0 << endl;
continue;
}
cout << query(rtrie, b, 0, lr.fi, lr.se) << endl;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
//cin >> t;
mp['A'] = 0;
mp['C'] = 1;
mp['G'] = 2;
mp['U'] = 3;
while(t--){
tc();
}
}
컴파일 시 표준 에러 (stderr) 메시지
selling_rna.cpp: In function 'int flip(int)':
selling_rna.cpp:39:22: warning: left shift count >= width of type [-Wshift-count-overflow]
39 | return ~(x) ^ (1 << 32);
| ~~^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |