#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;
}
vp stringnum;
vector<pair<P,P>> querynum;
class trie{
public:
vvi ch,q;
vp range;
ll c=1,euler;
vvi res;
trie(ll k){
ch=vvi(1,vi(sz,-1));
res=vvi(1);
}
void insert(ll num){
ll w=0;
rep(i,str[num].size()){
ll t=char_to_ll(str[num][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);
}
void add_query(ll num){
if(q.size()==0)q=vvi(c);
ll w=0;
rep(i,query[num].fi.size()){
ll t=char_to_ll(query[num].fi[i]);
if(ch[w][t]==-1)return;
else w=ch[w][t];
}
q[w].pb(num);
}
void euler_tour(ll w){
if(range.size()==0){
euler=0;
range=vp(c);
}
range[w].fi=euler++;
rep(i,sz)if(ch[w][i]!=-1)euler_tour(ch[w][i]);
range[w].se=euler;
for(ll x:res[w])stringnum[x].fi=range[w].fi;
for(ll x:q[w])querynum[x].fi=range[w];
}
};
class rtrie{
public:
vvi ch,q;
vp range;
ll c=1,euler;
vvi res;
rtrie(ll k){
ch=vvi(1,vi(sz,-1));
res=vvi(1);
}
void insert(ll num){
ll w=0;
for(int i=str[num].size()-1;i>=0;i--){
ll t=char_to_ll(str[num][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);
}
void add_query(ll num){
if(q.size()==0)q=vvi(c);
ll w=0;
for(int i=query[num].se.size()-1;i>=0;i--){
ll t=char_to_ll(query[num].se[i]);
if(ch[w][t]==-1)return;
else w=ch[w][t];
}
q[w].pb(num);
}
void euler_tour(ll w){
if(range.size()==0){
euler=0;
range=vp(c);
}
range[w].fi=euler++;
rep(i,sz)if(ch[w][i]!=-1)euler_tour(ch[w][i]);
range[w].se=euler;
for(ll x:res[w])stringnum[x].se=range[w].fi;
for(ll x:q[w])querynum[x].se=range[w];
}
};
int main(){
ll n,m;cin>>n>>m;
str=vector<string>(n);
query=vector<pair<string,string>>(m);
stringnum=vp(n);querynum=vector<pair<P,P>>(m);
ans=vi(m);
rep(i,n)cin>>str[i];
rep(i,m)cin>>query[i].fi>>query[i].se;
trie tr(1);
rtrie rtr(1);
rep(i,n){
tr.insert(i);
rtr.insert(i);
}
rep(i,m){
tr.add_query(i);
rtr.add_query(i);
}
tr.euler_tour(0);rtr.euler_tour(0);
rep(i,m)rep(j,n){
if(querynum[i].fi.fi<=stringnum[j].fi&&stringnum[j].fi<querynum[i].fi.se){
if(querynum[i].se.fi<=stringnum[j].se&&stringnum[j].se<querynum[i].se.se){
ans[i]++;
}
}
}
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 |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
583 ms |
269136 KB |
Output is correct |
2 |
Correct |
679 ms |
259544 KB |
Output is correct |
3 |
Correct |
581 ms |
269784 KB |
Output is correct |
4 |
Correct |
650 ms |
257248 KB |
Output is correct |
5 |
Correct |
754 ms |
331580 KB |
Output is correct |
6 |
Correct |
783 ms |
336476 KB |
Output is correct |
7 |
Correct |
188 ms |
14956 KB |
Output is correct |
8 |
Correct |
609 ms |
204988 KB |
Output is correct |
9 |
Correct |
549 ms |
174116 KB |
Output is correct |
10 |
Correct |
429 ms |
164236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1585 ms |
9704 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
583 ms |
269136 KB |
Output is correct |
9 |
Correct |
679 ms |
259544 KB |
Output is correct |
10 |
Correct |
581 ms |
269784 KB |
Output is correct |
11 |
Correct |
650 ms |
257248 KB |
Output is correct |
12 |
Correct |
754 ms |
331580 KB |
Output is correct |
13 |
Correct |
783 ms |
336476 KB |
Output is correct |
14 |
Correct |
188 ms |
14956 KB |
Output is correct |
15 |
Correct |
609 ms |
204988 KB |
Output is correct |
16 |
Correct |
549 ms |
174116 KB |
Output is correct |
17 |
Correct |
429 ms |
164236 KB |
Output is correct |
18 |
Execution timed out |
1585 ms |
9704 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |