답안 #341150

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
341150 2020-12-29T04:16:49 Z rrrr10000 Selling RNA Strands (JOI16_selling_rna) C++14
0 / 100
1500 ms 269176 KB
#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;
        rep(i,query[num].se.size()){
            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].fi.fi<=stringnum[j].fi&&stringnum[j].fi<=querynum[i].fi.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 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 522 ms 269176 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1556 ms 9704 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -