제출 #897071

#제출 시각아이디문제언어결과실행 시간메모리
897071trytovoiSelling RNA Strands (JOI16_selling_rna)C++14
100 / 100
725 ms740776 KiB
#include <bits/stdc++.h>
using namespace std;
 
#ifdef LOCAL
#include "debug2.h"
#else
#define debug(...) 42
#endif
 
template<typename T> bool ckmin(T& x, const T& y) { return x > y ? x = y, 1 : 0; } 
template<typename T> bool ckmax(T& x, const T& y) { return x < y ? x = y, 1 : 0; } 
 
#define REP(i, n) for(int i = 0, ____n = (n); i < ____n; ++i)
#define REPD(i, n) for(int i = (n) - 1; i >= 0; --i)
#define __builtin_popcount __builtin_popcountll
#define left  _______left
#define right _______right
#define MASK(x) (1LL << (x))
#define BIT(mask, x) (((mask) >> (x)) & 1)
#define SZ(x) (int) ((x).size())
#define ALL(x) (x).begin(), (x).end()
#define SQR(x) (1LL * (x) * (x))
#define BETWEEN(l, x, r) ((l) <= (x) && (x) <= (r))
 
const int dx[] = {-1, 1, 0, 0, -1, -1, 1, 1};
const int dy[] = {0, 0, -1, 1, -1, 1, -1, 1};
const int INF = (int) 1e9 + 7;
const long long LINF = (long long) 1e18 + 7;
const int MX = (int) 1e5 + 5;
const int BLOCK = 320;
 
struct node {
    node *nxt[30];
    vector<int> ids;
    node(void) {
        ids.clear();
        REP(i, 26) nxt[i] = NULL;
    }
};
 
node *root[2];
 
void addString(bool prefix, const string& s, int id) {
    node *cur = root[prefix];
    for (char ch : s) {
        int p = ch - 'A';
        if (cur->nxt[p] == NULL) cur->nxt[p] = new node();
        cur = cur->nxt[p];
        cur->ids.push_back(id);
    }
}
 
vector<int> getString(bool prefix, const string& s) {
    node *cur = root[prefix];
    for (char ch : s) {
        int p = ch - 'A';
        if (cur->nxt[p] == NULL) return vector<int>();
        cur = cur->nxt[p];
    }
    return cur->ids;
}
 
int n, q;
string S[MX];
 
signed main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    cin >> n >> q;
    REP(i, 2) root[i] = new node();
    REP(i, n) cin >> S[i];
    sort(S, S + n);
    REP(i, n) {
        addString(true, S[i], i);
        reverse(ALL(S[i]));
        addString(false, S[i], i);
    }
 
    while (q--) {
        string pref, suf; cin >> pref >> suf;
        reverse(ALL(suf));
        vector<int> vec_prefix = getString(true, pref);
        vector<int> vec_suffix = getString(false, suf);
 
        if (vec_prefix.empty() || vec_suffix.empty()) {
            cout << "0\n";
            continue;
        }
 
        int left = (int) (lower_bound(ALL(vec_suffix), vec_prefix[0]) - vec_suffix.begin());
        int right = (int) (upper_bound(ALL(vec_suffix), vec_prefix.back()) - vec_suffix.begin());
 
        cout << right - left << '\n';
    }
 
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...