Submission #846898

# Submission time Handle Problem Language Result Execution time Memory
846898 2023-09-08T15:44:35 Z thanh913 Selling RNA Strands (JOI16_selling_rna) C++14
100 / 100
1290 ms 248024 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
using ll = long long;
using pii = pair<int, int>;

const int N = 1e5+5, M = 2e6+5, B = 200, m1 = 1e9+7, m2 = 1e9+9, p = 53;

int mul1(ll x, ll y) {return x*y % m1;}
int mul2(ll x, ll y) {return x*y % m2;}

int pw1[N], pw2[N];
struct hasher {
    int n;
    vector<int> h1, h2;
    void init(string &s) {
        n = s.size();
        h1.assign(n+2, 0); h2.assign(n+2, 0);
        for (int i = n; i >= 1; i--) {
            h1[i] = (h1[i+1] + mul1(s[i-1], pw1[100000-i])) % m1;
            h2[i] = (h2[i+1] + mul2(s[i-1], pw2[100000-i])) % m2;
        }
    }
    pii get(int l) {
        return {mul1(h1[l], pw1[l]), mul2(h2[l], pw2[l])};
    }
};

//--------------------------------------
int n, m, ans[N];
string a[N], x[N], y[N];
hasher ha[N], hy[N];
pii vy[N];

struct trie {
    struct miniTrie {
        int n, ed[M];
        int nxt[M][4];
        void clear() {
            while (n >= 0) {
                ed[n] = nxt[n][0] = nxt[n][1] = nxt[n][2] = nxt[n][3] = 0;
                n--;
            }
            n = 0;
        }

        void add(string &s) {
            int id = 0;
            for (int i = s.size() - 1; i >= 0; i--) {
                if (!nxt[id][s[i]]) {
                    nxt[id][s[i]] = ++n;
                }
                id = nxt[id][s[i]]; ed[id]++;
            }
        }

        int get(string &s) {
            int id = 0;
            for (int i = s.size() - 1; i >= 0; i--) {
                if (!nxt[id][s[i]]) return 0;
                id = nxt[id][s[i]];
            }
            return ed[id];
        }
    } tree;

    int n, nxt[M][4];
    vector<int> idx[M], qr[M];

    void add(string &s, int idx) {
        int id = 0;
        for (auto u : s) {
            int &val = nxt[id][u];
            if (!val) val = ++n;
            id = val;
        }
        qr[id].push_back(idx);
    }

    vector<pii> vals;
    void solve(int id, int len) {
        for (auto u : idx[id]) {
            if (a[u].size() < B) {
                tree.add(a[u]);
            }
            else {
                for (auto v : qr[id]) {
                    if (a[u].size() < y[v].size()) continue;
                    int pos = a[u].size() - y[v].size() + 1;
                    if (ha[u].get(pos) == vy[v]) {
                        ans[v]++;
                    }
                }
            }
        }
        
        for (auto u : qr[id]) {
            if (y[u].size() >= B) continue;
            ans[u] += tree.get(y[u]);
        }
        tree.clear();

        for (auto u : idx[id]) {
            if (a[u].size() < len+1) continue;
            int &id2 = nxt[id][a[u][len]];
            if (!id2) id2 = ++n;
            idx[id2].push_back(u);
        }
        idx[id].resize(0);
        for (int i = 0; i < 4; i++) {
            if (nxt[id][i]) solve(nxt[id][i], len+1);
        }
    }
} tree;

void conv(string &s) {
    for (auto &u : s) {
        if (u == 'A') u = 0;
        if (u == 'G') u = 1;
        if (u == 'C') u = 2;
        if (u == 'U') u = 3;
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    pw1[0] = pw2[0] = 1;
    for (int i = 1; i < N; i++) {
        pw1[i] = mul1(pw1[i-1], p);
        pw2[i] = mul2(pw2[i-1], p);
    }
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i]; conv(a[i]);
        ha[i].init(a[i]);
        tree.idx[0].push_back(i);
    }
    for (int i = 1; i <= m; i++) {
        cin >> x[i] >> y[i];
        conv(x[i]); conv(y[i]);
        hy[i].init(y[i]); vy[i] = hy[i].get(1);
        tree.add(x[i], i);
    }
    tree.solve(0, 0);
    for (int i = 1; i <= m; i++) cout << ans[i] << '\n';
}

Compilation message

selling_rna.cpp: In member function 'void trie::miniTrie::add(std::string&)':
selling_rna.cpp:52:34: warning: array subscript has type 'char' [-Wchar-subscripts]
   52 |                 if (!nxt[id][s[i]]) {
      |                                  ^
selling_rna.cpp:53:33: warning: array subscript has type 'char' [-Wchar-subscripts]
   53 |                     nxt[id][s[i]] = ++n;
      |                                 ^
selling_rna.cpp:55:34: warning: array subscript has type 'char' [-Wchar-subscripts]
   55 |                 id = nxt[id][s[i]]; ed[id]++;
      |                                  ^
selling_rna.cpp: In member function 'int trie::miniTrie::get(std::string&)':
selling_rna.cpp:62:34: warning: array subscript has type 'char' [-Wchar-subscripts]
   62 |                 if (!nxt[id][s[i]]) return 0;
      |                                  ^
selling_rna.cpp:63:34: warning: array subscript has type 'char' [-Wchar-subscripts]
   63 |                 id = nxt[id][s[i]];
      |                                  ^
selling_rna.cpp: In member function 'void trie::add(std::string&, int)':
selling_rna.cpp:75:32: warning: array subscript has type 'char' [-Wchar-subscripts]
   75 |             int &val = nxt[id][u];
      |                                ^
selling_rna.cpp: In member function 'void trie::solve(int, int)':
selling_rna.cpp:106:29: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  106 |             if (a[u].size() < len+1) continue;
      |                 ~~~~~~~~~~~~^~~~~~~
selling_rna.cpp:107:41: warning: array subscript has type 'char' [-Wchar-subscripts]
  107 |             int &id2 = nxt[id][a[u][len]];
      |                                         ^
# Verdict Execution time Memory Grader output
1 Correct 36 ms 116856 KB Output is correct
2 Correct 26 ms 116828 KB Output is correct
3 Correct 28 ms 116632 KB Output is correct
4 Correct 27 ms 116632 KB Output is correct
5 Correct 28 ms 116828 KB Output is correct
6 Correct 27 ms 116844 KB Output is correct
7 Correct 27 ms 116828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 162 ms 149764 KB Output is correct
2 Correct 310 ms 156536 KB Output is correct
3 Correct 316 ms 248024 KB Output is correct
4 Correct 360 ms 243524 KB Output is correct
5 Correct 374 ms 199504 KB Output is correct
6 Correct 379 ms 200440 KB Output is correct
7 Correct 225 ms 164336 KB Output is correct
8 Correct 190 ms 202104 KB Output is correct
9 Correct 156 ms 195660 KB Output is correct
10 Correct 352 ms 180360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 124364 KB Output is correct
2 Correct 46 ms 121936 KB Output is correct
3 Correct 55 ms 123696 KB Output is correct
4 Correct 43 ms 122316 KB Output is correct
5 Correct 45 ms 122704 KB Output is correct
6 Correct 48 ms 123912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 116856 KB Output is correct
2 Correct 26 ms 116828 KB Output is correct
3 Correct 28 ms 116632 KB Output is correct
4 Correct 27 ms 116632 KB Output is correct
5 Correct 28 ms 116828 KB Output is correct
6 Correct 27 ms 116844 KB Output is correct
7 Correct 27 ms 116828 KB Output is correct
8 Correct 162 ms 149764 KB Output is correct
9 Correct 310 ms 156536 KB Output is correct
10 Correct 316 ms 248024 KB Output is correct
11 Correct 360 ms 243524 KB Output is correct
12 Correct 374 ms 199504 KB Output is correct
13 Correct 379 ms 200440 KB Output is correct
14 Correct 225 ms 164336 KB Output is correct
15 Correct 190 ms 202104 KB Output is correct
16 Correct 156 ms 195660 KB Output is correct
17 Correct 352 ms 180360 KB Output is correct
18 Correct 49 ms 124364 KB Output is correct
19 Correct 46 ms 121936 KB Output is correct
20 Correct 55 ms 123696 KB Output is correct
21 Correct 43 ms 122316 KB Output is correct
22 Correct 45 ms 122704 KB Output is correct
23 Correct 48 ms 123912 KB Output is correct
24 Correct 761 ms 158888 KB Output is correct
25 Correct 1290 ms 161616 KB Output is correct
26 Correct 613 ms 158308 KB Output is correct
27 Correct 575 ms 233040 KB Output is correct
28 Correct 662 ms 169132 KB Output is correct
29 Correct 136 ms 151240 KB Output is correct