Submission #319665

# Submission time Handle Problem Language Result Execution time Memory
319665 2020-11-06T04:40:57 Z qpwoeirut Selling RNA Strands (JOI16_selling_rna) C++17
35 / 100
1500 ms 154468 KB
#include <bits/stdc++.h>

using namespace std;

#define sz(x) (int)((x).size())

typedef long long ll;

const int MN = 100001;
const int CT = 2;
const ll MOD[CT] = {(ll)1e9 + 7, (ll)1e9+9};
const ll BASE = 11;

int to_int(char c) {
    if (c == 'A') return 1;
    else if (c == 'B') return 2;
    else if (c == 'C') return 3;
    else if (c == 'D') return 4;
    else if (c == 'G') return 5;
    else if (c == 'H') return 6;
    else if (c == 'U') return 7;
    else if (c == 'V') return 8;
    else {
        cerr << c << endl;
        assert(false);
    }
}

struct Hash {
    int h[CT];

    Hash() {
        fill(h, h+CT, 0);
    }
    Hash(string s) {
        fill(h, h+CT, 0);
        for (int i=sz(s) - 1; i>=0; --i) {
            for (int j=0; j<CT; ++j) {
                h[j] = ((h[j] * BASE) + to_int(s[i])) % MOD[j];
            }
        }
    }
    void add(char c) {
        int val = to_int(c);

        for (int i=0; i<CT; ++i) {
            h[i] = ((h[i] * BASE) + val) % MOD[i];
        }
    }
    inline const bool operator<(const Hash& other) const {
        for (int i=0; i<CT; ++i) {
            if (h[i] != other.h[i]) {
                return h[i] < other.h[i];
            }
        }
        return false;
    }
    inline const bool operator==(const Hash& other) const {
        for (int i=0; i<CT; ++i) {
            if (h[i] != other.h[i]) return false;
        }
        return true;
    }
};

struct Str {
    string s;
    vector<Hash> h;

    Str() {
        s = "";
        h = vector<Hash>();
    }
    Str(const string& str) {
        s = str;
        Hash cur;
        for (int i=0; i<sz(s); ++i) {
            cur.add(s[i]);
            h.emplace_back(cur);
        }
    }
    int size() {
        return s.size();
    }
    inline const bool operator<(const Str& other) {
        int mn = min(sz(s), sz(other.s));
        int lo = 0, hi = mn; 
        while (lo < hi) {
            int mid = (lo + hi) >> 1;
            if (h[mid] == other.h[mid]) {
                lo = mid + 1;
            } else {
                hi = mid;
            }
        }

        if (lo == mn) {
            return sz(s) < sz(other.s);
        }
        return s[lo] < other.s[lo];
    }
};

int N, M;
Str S[MN];
vector<Hash> phash[MN];
map<Hash, vector<int>> shash;

int main() {
    cin.tie(0)->sync_with_stdio(0);

    cin >> N >> M;
    for (int i=0; i<N; ++i) {
        string s;
        cin >> s;
        S[i] = Str(s);
    }
    sort(S, S+N);

    for (int i=0; i<N; ++i) {
        Hash cur;
        for (int j=sz(S[i]) - 1; j>=0; --j) {
            cur.add(S[i].s[j]);
            shash[cur].emplace_back(i);
        }
    }

    for (int i=0; i<M; ++i) {
        string P, Q;
        cin >> P >> Q;

        int lo = lower_bound(S, S+N, P) - S;
        P.back()++;
        int hi = lower_bound(S, S+N, P) - S;

        Hash suff(Q);
        auto it = shash.find(suff);
        if (it == shash.end()) {
            cout << "0\n";
        } else {
            int mn = lower_bound(it->second.begin(), it->second.end(), lo) - it->second.begin();
            int mx = lower_bound(it->second.begin(), it->second.end(), hi) - it->second.begin();

            cout << mx - mn << '\n';
        }
    }
}
/*
2 3
AUGC
AGC
G C
AU C
A C

3 3
AA
AA AGA
AA AA
AG GA
AG GA

8 7
GCGCUACCCCAACACAAGGCAAGAUAUA
G
GGAC
GCGG
U
GCGCUACCCCAACACAAGGCAAGAUGGUC
GCCG
GCGCUGA
GCGCUACCC A
GCGCUACCCC AC
GCG C
GCGC A
G G
G C
G GGA
*/
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8172 KB Output is correct
2 Correct 7 ms 8172 KB Output is correct
3 Correct 6 ms 8172 KB Output is correct
4 Correct 7 ms 8300 KB Output is correct
5 Correct 6 ms 8172 KB Output is correct
6 Correct 6 ms 8172 KB Output is correct
7 Correct 6 ms 8300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1541 ms 154468 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 196 ms 10720 KB Output is correct
2 Correct 164 ms 10852 KB Output is correct
3 Correct 177 ms 10856 KB Output is correct
4 Correct 179 ms 10472 KB Output is correct
5 Correct 136 ms 10596 KB Output is correct
6 Correct 191 ms 10852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8172 KB Output is correct
2 Correct 7 ms 8172 KB Output is correct
3 Correct 6 ms 8172 KB Output is correct
4 Correct 7 ms 8300 KB Output is correct
5 Correct 6 ms 8172 KB Output is correct
6 Correct 6 ms 8172 KB Output is correct
7 Correct 6 ms 8300 KB Output is correct
8 Execution timed out 1541 ms 154468 KB Time limit exceeded
9 Halted 0 ms 0 KB -