#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define fi first
#define se second
#define __builtin_popcount __builtin_popcountll
#define all(x) (x).begin(), (x).end()
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(x) ((1ll << (x)))
#define debug(a, l, r) for (int _i = (l); _i <= (r); ++_i) cout << (a)[_i] << ' '; cout << '\n';
const int MAXCHAR = 4;
const int INF = 1e9 + 7;
int encode(char c) {
if (c == 'A') return 0;
else if (c == 'C') return 1;
else if (c == 'G') return 2;
return 3;
}
char decode(int idx) {
if (idx == 0) return 'A';
else if (idx == 1) return 'C';
else if (idx == 2) return 'G';
return 'U';
}
struct Node{
Node *child[MAXCHAR];
int cntEnd = 0, l = INF, r = -INF;
vector<int> pos;
Node() {
for (int i = 0; i < MAXCHAR; ++i)
child[i] = nullptr;
}
};
struct Trie{
Node root;
Trie() {
root = Node();
}
void addString(string &s, int idx = -1) {
Node *cur = &root;
cur->l = min(cur->l, idx);
cur->r = max(cur->r, idx);
if (idx != -1) cur->pos.push_back(idx);
for (char c : s) {
int nxt = encode(c);
if (!cur->child[nxt]) cur->child[nxt] = new Node();
cur = cur->child[nxt];
cur->l = min(cur->l, idx);
cur->r = max(cur->r, idx);
if (idx != -1) cur->pos.push_back(idx);
}
cur->cntEnd++;
}
pii getRange(string &s) {
Node *cur = &root;
for (char c : s) {
int nxt = encode(c);
if (!cur->child[nxt]) return {-1, -1};
cur = cur->child[nxt];
}
return {cur->l, cur->r};
}
int count(string &s, int l, int r) {
Node *cur = &root;
for (char c : s) {
int nxt = encode(c);
if (!cur->child[nxt]) return 0;
cur = cur->child[nxt];
}
// for (auto v : cur->pos) cout << v << ' ';
// cout << '\n';
auto left_it = lower_bound(all(cur->pos), l);
auto right_it = upper_bound(all(cur->pos), r);
return (int)(right_it - left_it);
}
void sortStringDFS(Node* u, string cur, vector<string> &res) {
for (int i = 1; i <= u->cntEnd; ++i) res.push_back(cur);
for (int i = 0; i < MAXCHAR; ++i) {
if (!u->child[i]) continue;
sortStringDFS(u->child[i], cur + decode(i), res);
}
}
vector<string> sortString() {
vector<string> res;
sortStringDFS(&root, "", res);
return res;
}
};
const int MAXN = 1e5 + 5;
int n, m;
string words[MAXN];
void init() {
cin >> n >> m;
Trie trie;
for (int i = 1; i <= n; ++i) {
string s; cin >> s;
trie.addString(s);
}
vector<string> sortList = trie.sortString();
for (int i = 0; i < n; ++i) words[i + 1] = sortList[i];
}
void solve() {
Trie trie1, trie2;
for (int i = 1; i <= n; ++i) trie1.addString(words[i], i);
for (int i = 1; i <= n; ++i) {
string t = words[i];
reverse(all(t));
trie2.addString(t, i);
}
while (m--) {
string p, q; cin >> p >> q;
reverse(all(q));
pii get = trie1.getRange(p);
if (get == pii{-1, -1}) {
cout << 0 << '\n';
continue;
}
cout << trie2.count(q, get.fi, get.se) << '\n';
}
}
signed main() {
#ifdef NCTHANH
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(nullptr); cout.tie(nullptr);
init();
solve();
return 0;
}
| # | 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... |