Submission #598003

# Submission time Handle Problem Language Result Execution time Memory
598003 2022-07-17T09:18:45 Z isaachew Selling RNA Strands (JOI16_selling_rna) C++17
35 / 100
1500 ms 501708 KB
#include <bits/stdc++.h>
/*
 Trie+suffix trie
 
 ST1: Brute force (O(n^2m))
 ST2: trie + suffix trie (O(nm))
 ST3: Search in order of customers + suffix trie + HLD? O(sum(|S_i|)log N+sum(|P_i|)+sum(|Q_i|))
 */
class trie{
public:
    trie* nchar[4];
    bool leaf;
    int count;//total count
    int scount;//count in this node
    std::vector<int>* inps;
    trie():leaf(1),count(0),scount(0),inps(NULL){
        
    }
    ~trie(){
        if(!leaf){
            for(int i=0;i<4;i++){
                delete nchar[i];
            }
        }
        if(inps!=NULL){
            delete inps;
        }
    }
    void split(){
        if(!leaf)return;
        leaf=0;
        for(int i=0;i<4;i++){
            nchar[i]=new trie;
        }
    }
    void merge(trie* oth){
        scount+=oth->scount;
        count+=oth->count;
        if(oth->leaf)return;
        split();
        for(int i=0;i<4;i++){
            nchar[i]->merge(oth->nchar[i]);
        }
    }
    void add(const char* ch,int cnt=1){
        count+=cnt;
        if(ch[0]=='\0'){
            scount+=cnt;
        }else{
            split();
            int ind=ch[0]=='A'?0:ch[0]=='C'?1:ch[0]=='G'?2:3;
            return nchar[ind]->add(ch+1,cnt);
        }
    }
    trie* fnode(const char* ch){
        if(ch[0]=='\0'){
            return this;
        }
        int ind=ch[0]=='A'?0:ch[0]=='C'?1:ch[0]=='G'?2:3;
        return nchar[ind]->fnode(ch+1);
    }
    
    int fcnt(const char* ch){
        if(ch[0]=='\0'){
            return count;
        }
        int ind=ch[0]=='A'?0:ch[0]=='C'?1:ch[0]=='G'?2:3;
        return nchar[ind]->fcnt(ch+1);
    }
};

std::vector<int> outarr;
std::vector<std::pair<std::string,std::string>> prefs;
trie* recurse(trie* cur,std::string& cst){
    trie* sft;
    if(cur->leaf){
        sft=new trie;
    }else{
    
        trie* stries[4];
        int b_ind=0;
        int b_cnt=-1;
        for(int i=0;i<4;i++){
            cst+="ACGU"[i];
            stries[i]=recurse(cur->nchar[i],cst);
            cst.resize(cst.size()-1);
            if(stries[i]->count>b_cnt){
                b_cnt=stries[i]->count;
                b_ind=i;
            }
        }
        sft=stries[b_ind];
        for(int i=0;i<4;i++){
            if(i==b_ind)continue;
            sft->merge(stries[i]);
            delete stries[i];
        }
    }
    if(cur->scount){//add reverse string
        char* rst=new char[cst.size()+1];
        for(int i=0;i<cst.size();i++){
            rst[i]=cst[cst.size()-i-1];
        }
        rst[cst.size()]='\0';//null terminate
        sft->add(rst,cur->scount);
        delete[] rst;
    }
    if(cur->inps==NULL)return sft;
    for(int i:*cur->inps){
        std::string x=prefs[i].second;
        char* rst=new char[x.size()+1];
        for(int j=0;j<x.size();j++){
            rst[x.size()-j-1]=x[j];
        }
        rst[x.size()]='\0';
        outarr[i]=sft->fcnt(rst);
    }
    return sft;
}
int main(){
    int n,m;
    std::cin>>n>>m;
    std::vector<std::string> strs;
    trie root;
    for(int i=0;i<n;i++){
        std::string x;
        std::cin>>x;
        root.add(x.c_str());
        strs.push_back(x);
    }
    for(int i=0;i<m;i++){
        std::string x,y;
        std::cin>>x>>y;
        prefs.push_back({x,y});
        trie* ftr=root.fnode(x.c_str());
        if(ftr->inps==NULL){
            ftr->inps=new std::vector<int>();
            
        }
        ftr->inps->push_back(i);
    }
    outarr.resize(m);
    std::string st;
    trie* ftr=recurse(&root,st);
    delete ftr;
    for(int i=0;i<m;i++){
        std::cout<<outarr[i]<<'\n';
    }
}

Compilation message

selling_rna.cpp: In function 'trie* recurse(trie*, std::string&)':
selling_rna.cpp:101:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |         for(int i=0;i<cst.size();i++){
      |                     ~^~~~~~~~~~~
selling_rna.cpp:112:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |         for(int j=0;j<x.size();j++){
      |                     ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1207 ms 498408 KB Output is correct
2 Correct 1122 ms 471244 KB Output is correct
3 Execution timed out 1575 ms 501708 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 33 ms 8156 KB Output is correct
2 Correct 29 ms 5640 KB Output is correct
3 Correct 35 ms 7928 KB Output is correct
4 Correct 30 ms 5920 KB Output is correct
5 Correct 30 ms 5744 KB Output is correct
6 Correct 34 ms 8168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1207 ms 498408 KB Output is correct
9 Correct 1122 ms 471244 KB Output is correct
10 Execution timed out 1575 ms 501708 KB Time limit exceeded
11 Halted 0 ms 0 KB -