Submission #624370

#TimeUsernameProblemLanguageResultExecution timeMemory
624370fdnfksdSelling RNA Strands (JOI16_selling_rna)C++14
100 / 100
331 ms250316 KiB
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const ll maxN=2e5;
ll n,q,a[maxN];
struct TrieNode {
    TrieNode* child[4];
    vector<ll> pos;
    TrieNode() {
        for (int i=0; i<4; ++i)
            child[i]=NULL;
        pos.clear();
    }
};
ll val[300];
TrieNode *root=new TrieNode();
TrieNode *root1=new TrieNode();
#define pb push_back
void TrieInsert(const string &s, ll id)
{
    int nn=s.length();
    TrieNode* p=root;
    for (int i=0; i<nn; ++i) {
        int nxt=val[s[i]];
        if (p->child[nxt]==NULL)
            p->child[nxt]=new TrieNode();
        p=p->child[nxt];
        p->pos.pb(id);
    }
}
void add(const string &s, ll id)
{
    int nn=s.length();
    TrieNode* p=root1;
    for (int i=0; i<nn; ++i) {
        int nxt=val[s[i]];
        if (p->child[nxt]==NULL)
            p->child[nxt]=new TrieNode();
        p=p->child[nxt];
        p->pos.pb(id);
    }
}
bool cmp(string x, string y)
{
    reverse(x.begin(),x.end());
    reverse(y.begin(),y.end());
    for(int i=0;i<min(x.length(),y.length());i++)
    {
        if(x[i]<y[i]) return true;
        if(x[i]>y[i]) return false;
    }
    if(x.length()<y.length())return true;
    return false;
}

ll m;
string s[maxN],t[maxN];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >>n>>m;
    val['A']=0;
    val['G']=1;
    val['C']=2;
    val['U']=3;
    for(int i=1;i<=n;i++)
    {
        cin >> s[i];
    }
    sort(s+1,s+n+1,cmp);
    for(int i=1;i<=n;i++)
    {
        t[i]=s[i];
        reverse(t[i].begin(),t[i].end());
        add(t[i],i);
    }
    for(int i=1;i<=n;i++)
    {
        TrieInsert(s[i],i);
    }
    string p,q;
    for(int i=1;i<=m;i++)
    {
        cin >> p >> q;
        reverse(q.begin(),q.end());
        TrieNode *x=root1;
        bool ok=true;
        for(int j=0;j<q.size();j++)
        {
            ll nxt=val[q[j]];
            if(x->child[nxt]==nullptr)
            {
                ok=false;
                break;
            }
            else x=x->child[nxt];
        }
        ll l=0,r=0;
        if(ok&&x->pos.size()>0)
        {
            l=x->pos[0];
            r=x->pos.back();
        }
        TrieNode *pp=root;
        for(int j=0;j<p.size();j++)
        {
            ll nxt=val[p[j]];
            if(pp->child[nxt]==nullptr)
            {
                ok=false;
                break;
            }
            else pp=pp->child[nxt];
        }
        if(ok&&pp->pos.size()>0)
        {
            cout << upper_bound(pp->pos.begin(),pp->pos.end(),r)-lower_bound(pp->pos.begin(),pp->pos.end(),l)<<'\n';
        }
        else
        {
            cout <<0<<'\n';
        }
    }
}

Compilation message (stderr)

selling_rna.cpp: In function 'void TrieInsert(const string&, ll)':
selling_rna.cpp:24:25: warning: array subscript has type 'char' [-Wchar-subscripts]
   24 |         int nxt=val[s[i]];
      |                         ^
selling_rna.cpp: In function 'void add(const string&, ll)':
selling_rna.cpp:36:25: warning: array subscript has type 'char' [-Wchar-subscripts]
   36 |         int nxt=val[s[i]];
      |                         ^
selling_rna.cpp: In function 'bool cmp(std::string, std::string)':
selling_rna.cpp:47:18: warning: comparison of integer expressions of different signedness: 'int' and 'const long unsigned int' [-Wsign-compare]
   47 |     for(int i=0;i<min(x.length(),y.length());i++)
      |                 ~^~~~~~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:89:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |         for(int j=0;j<q.size();j++)
      |                     ~^~~~~~~~~
selling_rna.cpp:91:28: warning: array subscript has type 'char' [-Wchar-subscripts]
   91 |             ll nxt=val[q[j]];
      |                            ^
selling_rna.cpp:106:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |         for(int j=0;j<p.size();j++)
      |                     ~^~~~~~~~~
selling_rna.cpp:108:28: warning: array subscript has type 'char' [-Wchar-subscripts]
  108 |             ll nxt=val[p[j]];
      |                            ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...