이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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) 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);
        reverse(all(suf));
        pii tmp = find1(pref);
        int ans = find2(suf, tmp);
        cout << ans << '\n';
    }
}
int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    init();
    solve();
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |