답안 #710647

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
710647 2023-03-15T13:09:39 Z vjudge1 Selling RNA Strands (JOI16_selling_rna) C++14
100 / 100
520 ms 231892 KB
#include <bits/stdc++.h>
using namespace std;

#define tcT template<class T
#define each(e, a) for(auto &(e) : (a))
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sz(v) (int)(v).size()
#define nl '\n';

using ll = long long;
tcT> bool ckmin(T&a, const T&b) { return b < a ? a = b, 1 : 0; }
tcT> bool ckmax(T&a, const T&b) { return b > a ? a = b, 1 : 0; }

const ll INF = 1e18; const int MOD = 1e9 + 7;

const int MX = 1e5+5;

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

char get_char(char c) {
    if(c == 0) return 'A';
    else if(c == 1) return 'C';
    else if(c == 2) return 'G';
    return 'U';
}

struct Trie {
    vector<array<int,4>> nex = {{{}}};
    vector<vector<int>> lst = {{}};
    vector<int> cnt = {0};
    void ins(const string&s, int ind) {
        int cur = 0;
        for(int i = 0; i < sz(s); i++) {
            char c = getid(s[i]);
            if(!nex[cur][c]) {
                nex[cur][c] = sz(nex);
                nex.push_back({{{}}});
                lst.push_back({});
                cnt.push_back(0);
            }
            cur = nex[cur][c];
            lst[cur].push_back(ind);
        }
        cnt[cur]++;
    }
    void dfs(int u, string&cur, vector<string>& anslst) {
        for(int i = 0; i < cnt[u]; i++) {
            anslst.push_back(cur);
        }

        for(int i = 0; i < 4; i++) {
            if(nex[u][i]) {
                cur += get_char(i);
                dfs(nex[u][i], cur, anslst);
                cur.pop_back();
            }
        }
    }

    pair<int,int> getlr(const string&s) {
        int cur = 0;
        for(int i = 0; i < sz(s); i++) {
            int c = getid(s[i]);
            if(!nex[cur][c]) return {-1, -1};
            cur = nex[cur][c];
        }
        return {*lst[cur].begin(), *lst[cur].rbegin()};
    }

    int get_cnt(const string&s, int l, int r) {
        int cur = 0;
        for(int i = 0; i < sz(s); i++) {
            int c = getid(s[i]);
            if(!nex[cur][c]) return 0;
            cur = nex[cur][c];
        }
        return upper_bound(all(lst[cur]), r) - lower_bound(all(lst[cur]), l);
    }
};

int N, M;
string s[MX];

void sort_all() {
    Trie trie;
    for(int i = 0; i < N; i++) {
        trie.ins(s[i], i);
    }
    vector<string> lst;
    string cur = "";
    trie.dfs(0, cur, lst);
    for(int i = 0; i < N; i++) s[i] = lst[i];
}



void solve() {
    cin >> N >> M;
    for(int i = 0; i < N; i++) {
        cin >> s[i];
    }
    sort_all();

    Trie trie1;
    for(int i = 0; i < N; i++) {
        trie1.ins(s[i], i);
        reverse(all(s[i]));
    }

    Trie trie2;
    for(int i = 0; i < N; i++) {
        trie2.ins(s[i], i);
    }

    while(M-- > 0) {
        string P, Q;
        cin >> P >> Q;
        reverse(all(Q));
        int l, r; tie(l, r) = trie1.getlr(P);
        if(~l) { cout << trie2.get_cnt(Q, l, r) << nl; }
        else cout << "0\n";
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 3 ms 3412 KB Output is correct
4 Correct 3 ms 3412 KB Output is correct
5 Correct 4 ms 3412 KB Output is correct
6 Correct 2 ms 3452 KB Output is correct
7 Correct 2 ms 3412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 320 ms 164884 KB Output is correct
2 Correct 341 ms 160948 KB Output is correct
3 Correct 491 ms 182172 KB Output is correct
4 Correct 507 ms 191940 KB Output is correct
5 Correct 465 ms 229828 KB Output is correct
6 Correct 520 ms 231892 KB Output is correct
7 Correct 82 ms 27652 KB Output is correct
8 Correct 348 ms 147392 KB Output is correct
9 Correct 311 ms 131188 KB Output is correct
10 Correct 265 ms 130344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 6088 KB Output is correct
2 Correct 24 ms 5536 KB Output is correct
3 Correct 28 ms 6568 KB Output is correct
4 Correct 19 ms 6516 KB Output is correct
5 Correct 27 ms 5744 KB Output is correct
6 Correct 25 ms 6728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 3 ms 3412 KB Output is correct
4 Correct 3 ms 3412 KB Output is correct
5 Correct 4 ms 3412 KB Output is correct
6 Correct 2 ms 3452 KB Output is correct
7 Correct 2 ms 3412 KB Output is correct
8 Correct 320 ms 164884 KB Output is correct
9 Correct 341 ms 160948 KB Output is correct
10 Correct 491 ms 182172 KB Output is correct
11 Correct 507 ms 191940 KB Output is correct
12 Correct 465 ms 229828 KB Output is correct
13 Correct 520 ms 231892 KB Output is correct
14 Correct 82 ms 27652 KB Output is correct
15 Correct 348 ms 147392 KB Output is correct
16 Correct 311 ms 131188 KB Output is correct
17 Correct 265 ms 130344 KB Output is correct
18 Correct 23 ms 6088 KB Output is correct
19 Correct 24 ms 5536 KB Output is correct
20 Correct 28 ms 6568 KB Output is correct
21 Correct 19 ms 6516 KB Output is correct
22 Correct 27 ms 5744 KB Output is correct
23 Correct 25 ms 6728 KB Output is correct
24 Correct 287 ms 142024 KB Output is correct
25 Correct 290 ms 142384 KB Output is correct
26 Correct 295 ms 140496 KB Output is correct
27 Correct 486 ms 170168 KB Output is correct
28 Correct 195 ms 46480 KB Output is correct
29 Correct 85 ms 17260 KB Output is correct