#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
#define ykh mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count())
#define __lg(x) 63-__builtin_clzll(x)
#define pow2(x) (1LL<<x)
void __print(int x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef local
void setio(){freopen("/Users/iantsai/Library/Mobile Documents/com~apple~CloudDocs/cpp/Empty.md","r",stdin);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
void setio(){}
#define debug(x...)
#endif
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
struct line{
int a,b;
int operator()(const int x)const{
return a*x+b;
}
};
bool check(line l1,line l2,line l3){
return (l3.b-l2.b)*(l1.a-l2.a)<=(l2.b-l1.b)*(l2.a-l3.a);
}
const int mxn=1e5+5;
const int mxl=2e6+5;
int n, m;
string s[mxn];
int agcu(char c){
if(c=='A')return 0;
if(c=='G')return 1;
if(c=='C')return 2;
return 3;
}
struct trie{
int timer=0, nxt[mxl][4], cnt[mxl], euler, in[mxl], out[mxl];
void init(){
timer=0;
euler=1;
memset(nxt,0,sizeof(nxt));
memset(cnt,0,sizeof(cnt));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
}
void ins(string s){
int cur=0;
for(auto c:s){
int x=agcu(c);
if(!nxt[cur][x]){
nxt[cur][x]=++timer;
}
cur=nxt[cur][x];
debug(cur);
}
cnt[cur]++;
}
void dfs(int v){
in[v]=euler;
euler+=cnt[v];
out[v]=euler-1;
for(int i=0;i<4;i++){
if(nxt[v][i]){
dfs(nxt[v][i]);
out[v]=out[nxt[v][i]];
}
}
}
pii query(string s){
int cur=0;
for(auto c:s){
int x=agcu(c);
if(nxt[cur][x]){
cur=nxt[cur][x];
}
else{
return {-1,-1};
}
}
return {in[cur],out[cur]};
}
}tr,rt;
struct BIT{
int bit[mxn];
void init(){
memset(bit,0,sizeof(bit));
}
void modify(int p,int v){
for(;p<mxn;p+=p&-p){
bit[p]+=v;
}
}
int query(int p){
int res=0;
for(;p;p-=p&-p){
res+=bit[p];
}
return res;
}
int query(int l,int r){
return query(r)-query(l-1);
}
};
vector<tuple<int,int,int>>in[mxn],out[mxn],mod[mxn];
signed main(){
setio();
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>m;
tr.init();
rt.init();
for(int i=1;i<=n;i++){
cin>>s[i];
tr.ins(s[i]);
reverse(all(s[i]));
rt.ins(s[i]);
}
tr.dfs(0);
rt.dfs(0);
for(int i=1;i<=n;i++){
pii sec=rt.query(s[i]);
reverse(all(s[i]));
pii f=tr.query(s[i]);
mod[f.F].pb({sec.F,0,0});
}
for(int i=1;i<=m;i++){
string a,b;
cin>>a>>b;
pii f=tr.query(a);
pii sec=rt.query(b);
if(f.F<0 or sec.F<0)continue;
in[f.F].pb({i,sec.F,sec.S});
out[f.S].pb({i,sec.F,sec.S});
}
BIT bt;
bt.init();
vector<int>ans(m+1);
for(int i=1;i<=n;i++){
for(auto [id,l,r]:in[i]){
ans[id]-=bt.query(l,r);
}
for(auto [p,l,r]:mod[i]){
debug(p);
bt.modify(p,1);
}
for(auto [id,l,r]:out[i]){
ans[id]+=bt.query(l,r);
}
}
for(int i=1;i<=m;i++){
cout<<ans[i]<<'\n';
}
}
/*
input:
*/
Compilation message
selling_rna.cpp: In function 'void setIO(std::string)':
selling_rna.cpp:42:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:43:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
32 ms |
230480 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
97 ms |
233020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
48 ms |
235328 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
32 ms |
230480 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |