Submission #1279914

#TimeUsernameProblemLanguageResultExecution timeMemory
1279914nhmktuSelling RNA Strands (JOI16_selling_rna)C++17
100 / 100
201 ms233832 KiB
#include <bits/stdc++.h>
#define ll long long
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define pi pair<int,int>
#define pii pair<int,pi>
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(a) (int) a.size()
#define endl '\n'
#define data "data"

using namespace std;
const ll linf = 1e18;
const int inf = 1e9;
const int MOD = 1e9 + 7, N = 1e5;

void add(int &a, int b)
{
    a += b;
    if(a>=MOD)
        a-=MOD;
    if(a<0)
        a += MOD;
}

int modulo(int x)
{
    if(x<=1)
        return 1;
    return (MOD - MOD/x) * modulo(MOD/x) % MOD;
}

int mul(int a, int b)
{
    return (1ll *a%MOD * b%MOD) % MOD;
}

int n, m;
vector<string> Vs;

void inp(void)
{
    cin >> n >> m;
    FOR(i, 1, n)
    {
        string s; cin >> s;
        Vs.pb(s);
    }
    sort(all(Vs));
}

struct Trie
{
    Trie *child[4];
    int l, r;
    vector<int> idx;

    Trie()
    {
        l = r = - 1;
        idx = {};
        FOR(i, 0, 3) child[i] = NULL;
    }
};

Trie *root1;
Trie *root2;

int cnv(char c)
{
    if(c == 'A') return 0;
    if(c == 'U') return 1;
    if(c == 'G') return 2;
    if(c == 'C') return 3;
}

void Insert(string &s, int id, bool f)
{
    Trie *p;
    if(f)
        p = root1;
    else
        p = root2;

    FOR(i, 0, sz(s) -1)
    {
        int c = cnv(s[i]);
        if(p -> child[ c ] == NULL) p -> child[c] = new Trie();
        p = p -> child[c];
        if(f)
        {
            if(p -> l == -1) p -> l = id;
            p -> r = max(p -> r, id);
        }
        else
            (p -> idx).pb(id);
    }
}

pi Get1(string &s)
{
    Trie *p = root1;
    FOR(i, 0, sz(s) -1)
    {
        int c = cnv(s[i]);
        if(p -> child[c] == NULL) return {-1, -1};
        p = p -> child[c];
    }
    return {p -> l, p -> r};
}

int Get2(string &s, int L, int R)
{
    Trie *p = root2;
    FOR(i, 0, sz(s) -1)
    {
        int c = cnv(s[i]);
        if(p -> child[c] == NULL)
        {
            return 0;
        }
        p = p -> child[c];
    }

    vector<int> :: iterator p1 = lower_bound(all(p -> idx), L);
    vector<int> :: iterator p2 = upper_bound(all(p -> idx), R);
    return p2 - p1;
}

void solve(void)
{
    root1 = new Trie();
    root2 = new Trie();

    FOR(i, 0, sz(Vs) -1)
        Insert(Vs[i], i, 1);

    FOR(i, 0, sz(Vs) -1)
    {
        string &s = Vs[i];
        reverse(all(s));
        Insert(s, i, 0);
    }

    FOR(i, 1, m)
    {
        string pre, suf; cin >> pre >> suf;
        pi ret = Get1(pre);
        int L = ret.fi, R = ret.se;

        if(L == -1 && R == -1)
        {
            cout << "0\n";
        }
        else
        {
            reverse(all(suf));
            cout << Get2(suf, L, R) << '\n';
        }
    }
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    if(fopen(data".inp", "r"))
    {
        freopen(data".inp","r",stdin);
        freopen(data".out","w",stdout);
    }

    inp();
    solve();
    return 0;
}

Compilation message (stderr)

selling_rna.cpp: In function 'int cnv(char)':
selling_rna.cpp:78:1: warning: control reaches end of non-void function [-Wreturn-type]
   78 | }
      | ^
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:173:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  173 |         freopen(data".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:174:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  174 |         freopen(data".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...