답안 #497642

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
497642 2021-12-23T12:47:32 Z khoabright Selling RNA Strands (JOI16_selling_rna) C++17
0 / 100
200 ms 194300 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
#define rep(i, a, b) for (int i = (int)a; i <= (int)b; ++i)
#define rep1(i, a, b) for (int i = (int)a; i >= (int)b; --i)
#define all(x) x.begin(), x.end()
#define all1(x) x.begin() + 1, x.end()

const int N = 1e5 + 5;
const int inf = 1e9;
int n, m;

string s[N];

struct node1 {
    node1* child[4] = {};
    int left = inf, right = -inf;
};

struct node2 {
    node2* child[4] = {};
    vector<int> pos;
};

node1* root1 = new node1;
node2* root2 = new node2;

void add1(string ss, int idx) {
    node1* cur = root1;
    for (char c : ss) {
        int x = c - 'a';
        if (!cur -> child[x]) cur -> child[x] = new node1;
        cur = cur -> child[x];
        cur -> left = min(cur -> left, idx);
        cur -> right = max(cur -> right, idx);
    }
}

void add2(string ss, int idx) {
    node2* cur = root2;
    for (char c : ss) {
        int x = c - 'a';
        if (!cur -> child[x]) cur -> child[x] = new node2;
        cur = cur -> child[x];
        cur -> pos.pb(idx);
    }
}

pii find1(string s) {
    node1* cur = root1;
    for (char c : s) {
        int x = c - 'a';
        if (!cur -> child[x]) return {-1, -1};
        cur = cur -> child[x];
    }
    return {cur -> left, cur -> right};
}

int find2(string s, pii range) {
    if (range.ff == -1) return 0;
    vector<int> res;
    node2* cur = root2;
    for (char c : s) {
        int x = c - 'a';
        if (!cur -> child[x]) return 0;
        cur = cur -> child[x];
    }

    auto l = lower_bound(all(cur -> pos), range.ff);
    auto r = upper_bound(all(cur -> pos), range.ss);

    return r - l;
}

bool cmp(string &a, string &b) {
    int sz = min(a.size(), b.size());
    rep(i, 0, sz - 1) {
        if (a[i] != b[i]) return a[i] < b[i];
    }
    return a.size() < b.size();
}

void convert(string &t) {
    rep(i, 0, t.size() - 1) {
        if (t[i] == 'A') t[i] = 'a';
        if (t[i] == 'C') t[i] = 'b';
        if (t[i] == 'G') t[i] = 'c';
        if (t[i] == 'U') t[i] = 'd';
    }
}

void init() {
    cin >> n >> m;
    rep(i, 1, n) {
        cin >> s[i];
        convert(s[i]);
    }
    sort(s + 1, s + n + 1, cmp);
    //rep(i, 1, n) cout << s[i] << '\n';

    rep(i, 1, n) add1(s[i], i);
    rep(i, 1, n) reverse(all(s[i]));
    rep(i, 1, n) add2(s[i], i);
}

void solve() {
    rep(_m, 1, m) {
        string pref, suf; cin >> pref >> suf;
        convert(pref);
        convert(suf);
        pii tmp = find1(pref);
        //cout<<"tmp="<<tmp.ff<<' '<<tmp.ss<<'\n';
        int ans = find2(suf, tmp);
        cout << ans << '\n';
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); 
    init();
    solve();
}



# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 3408 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 200 ms 194300 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 25 ms 4616 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 3408 KB Output isn't correct
2 Halted 0 ms 0 KB -