#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define all(a) a.begin(),a.end()
#define rsort(a) {sort(all(a));reverse(all(a));}
#define pb emplace_back
#define lb(v,k) (lower_bound(all(v),(k))-v.begin())
#define fi first
#define se second
#define dame(a) {out(a);return 0;}
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
using vi=vector<ll>;
using vvi=vector<vi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
const ll sz=4;
vector<string> str;
vector<pair<string,string>> query;
vi ans;
ll char_to_ll(char c){
if(c=='A')return 0;
if(c=='G')return 1;
if(c=='C')return 2;
if(c=='U')return 3;
}
class trie{
public:
vector<string> all_str;
vvi ch;
ll c=1,lengthsum=0;
vvi res;
trie(ll k){
ch=vvi(1,vi(sz,-1));
res=vvi(1);
}
void insert(string&s,int num){
lengthsum+=s.size();
all_str.pb(s);
ll w=0;
rep(i,s.size()){
ll t=char_to_ll(s[i]);
if(ch[w][t]==-1){
ch[w][t]=c++;
res.pb(vi(0));ch.pb(vi(4,-1));
w=ch[w][t];
}
else{
w=ch[w][t];
}
res[w].pb(num);
}
}
ll getnum(string&s){
ll w=0;
rep(i,s.size()){
ll t=char_to_ll(s[i]);
if(ch[w][t]==-1)return 0;
else w=ch[w][t];
}
return res[w].size();
}
};
class rtrie{
public:
vvi q;
vvi ch;
ll c=1;
vector<trie> res;
rtrie(ll k){
ch=vvi(1,vi(sz,-1));
res.pb(trie(k));
}
void insert(string&s){
ll w=0;
for(int i=s.size()-1;i>=0;i--){
ll t=char_to_ll(s[i]);
if(ch[w][t]==-1){
ch[w][t]=c++;
res.pb(trie(0));ch.pb(vi(4,-1));
w=ch[w][t];
}
else{
w=ch[w][t];
}
}
res[w].insert(s,0);
}
void add_query(string&s,ll id){
if(q.size()==0)q=vvi(c);
ll w=0;
for(int i=s.size()-1;i>=0;i--){
ll t=char_to_ll(s[i]);
if(ch[w][t]==-1)return;
else w=ch[w][t];
}
q[w].pb(id);
}
void solve(ll w){
rep(i,4)if(ch[w][i]!=-1){
ll t=ch[w][i];
solve(t);
if(res[w].lengthsum<res[t].lengthsum)swap(res[w],res[t]);
for(string x:res[t].all_str)res[w].insert(x,0);
}
for(ll x:q[w])ans[x]+=res[w].getnum(query[x].fi);
}
};
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
int main(){
cin.tie();
ios::sync_with_stdio(false);
ll n,m;cin>>n>>m;
str=vector<string>(n);
query=vector<pair<string,string>>(m);
ans=vi(m);
rep(i,n)cin>>str[i];
rep(i,m)cin>>query[i].fi>>query[i].se;
rtrie rv(0);
rep(i,n)rv.insert(str[i]);
rep(i,m)rv.add_query(query[i].se,i);
rv.solve(0);
for(ll x:ans)out(x);
}
Compilation message
selling_rna.cpp: In function 'll char_to_ll(char)':
selling_rna.cpp:40:1: warning: control reaches end of non-void function [-Wreturn-type]
40 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1670 ms |
942520 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
10456 KB |
Output is correct |
2 |
Correct |
47 ms |
14800 KB |
Output is correct |
3 |
Correct |
52 ms |
14404 KB |
Output is correct |
4 |
Correct |
24 ms |
9300 KB |
Output is correct |
5 |
Correct |
39 ms |
12948 KB |
Output is correct |
6 |
Correct |
43 ms |
13532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Execution timed out |
1670 ms |
942520 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |