Submission #493167

# Submission time Handle Problem Language Result Execution time Memory
493167 2021-12-10T08:41:54 Z alextodoran Selling RNA Strands (JOI16_selling_rna) C++17
10 / 100
1500 ms 424260 KB
/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 100000;

const int MOD = (int) 1e9 + 7;

int N, M;

string S[N_MAX + 2];
string P[N_MAX + 2], Q[N_MAX + 2];

int Ssum, Psum, Qsum;
int T;

map <char, int> code = {
    {'A', 1}, {'C', 2},
    {'G', 3}, {'U', 4}
};

vector <int> getPrefHash (string s) {
    vector <int> prefHash ((int) s.size());
    for (int i = 0; i < (int) s.size(); i++) {
        prefHash[i] = (i - 1 >= 0 ? prefHash[i - 1] : 0);
        prefHash[i] = ((ll) 5 * prefHash[i] + code[s[i]]) % MOD;
    }
    return prefHash;
}
vector <int> getSuffHash (string s) {
    vector <int> suffHash ((int) s.size());
    for (int i = (int) s.size() - 1; i >= 0; i--) {
        suffHash[i] = (i + 1 < (int) s.size() ? suffHash[i + 1] : 0);
        suffHash[i] = ((ll) 5 * suffHash[i] + code[s[i]]) % MOD;
    }
    return suffHash;
}

vector <int> sprefHash[N_MAX + 2];
vector <int> ssuffHash[N_MAX + 2];

map <pair <int, int>, int> cnt;

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

    cin >> N >> M;
    for (int i = 0; i < N; i++) {
        cin >> S[i];
        Ssum += (int) S[i].size();
    }
    for (int i = 0; i < M; i++) {
        cin >> P[i] >> Q[i];
        Psum += (int) P[i].size();
        Qsum += (int) Q[i].size();
    }

    T = sqrt((ll) Ssum * max(Psum, Qsum) * N);

    for (int i = 0; i < N; i++) {
        sprefHash[i] = getPrefHash(S[i]);
        ssuffHash[i] = getSuffHash(S[i]);
        for (int l = 1; l <= T; l++) {
            for (int r = 1; r <= T && max(l, r) <= (int) S[i].size(); r++) {
                cnt[{sprefHash[i][l - 1], ssuffHash[i][(int) S[i].size() - r]}]++;
            }
        }
    }

    for (int i = 0; i < M; i++) {
        int phash = getPrefHash(P[i]).back();
        int qhash = getSuffHash(Q[i]).front();
        if ((int) P[i].size() <= T && (int) Q[i].size() <= T) {
            cout << cnt[{phash, qhash}] << "\n";
        } else {
            int answer = 0;
            for (int j = 0; j < N; j++) {
                if (max((int) P[i].size(), (int) Q[i].size()) <= (int) S[j].size()) {
                    if (phash == sprefHash[j][(int) P[i].size() - 1]
                     && qhash == ssuffHash[j][(int) S[j].size() - (int) Q[i].size()]) {
                        answer++;
                    }
                }
            }
            cout << answer << "\n";
        }
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14416 KB Output is correct
2 Correct 8 ms 14416 KB Output is correct
3 Correct 8 ms 14416 KB Output is correct
4 Correct 8 ms 14448 KB Output is correct
5 Correct 9 ms 14416 KB Output is correct
6 Correct 8 ms 14416 KB Output is correct
7 Correct 8 ms 14416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1600 ms 424260 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1532 ms 14960 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14416 KB Output is correct
2 Correct 8 ms 14416 KB Output is correct
3 Correct 8 ms 14416 KB Output is correct
4 Correct 8 ms 14448 KB Output is correct
5 Correct 9 ms 14416 KB Output is correct
6 Correct 8 ms 14416 KB Output is correct
7 Correct 8 ms 14416 KB Output is correct
8 Execution timed out 1600 ms 424260 KB Time limit exceeded
9 Halted 0 ms 0 KB -