#include "bits/stdc++.h"
using namespace std;
#define task ""
#define ll long long
#define endl '\n'
#define fi first
#define se second
#define vall(a) (a).begin(), (a).end()
#define sze(a) (int)a.size()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ep emplace_back
#define pb push_back
#define pf push_front
const ll mod = 1e9 + 7;
const int N = 5e5 + 5;
const ll oo = 1e18;
bool START;
int n, q;
string s[N];
vector<int> qr[N];
int sad(char ch) {
if (ch == 'A') return 0;
if (ch == 'U') return 1;
if (ch == 'G') return 2;
return 3;
}
struct Trie {
int in[N], out[N], t = 0;
struct node {
node *child[4];
int in, out;
vector<int> ids;
node() {
for (int i = 0; i < 4; ++i) child[i] = nullptr;
in = out = -1;
}
} Node[N];
int curnode = 0;
node *newnode() {
return &Node[curnode++];
}
node *root = newnode();
void add(const string x, int id, bool type) {
node *p = root;
for (char ch : x) {
int val = sad(ch);
if (p->child[val] == nullptr) p->child[val] = newnode();
p = p->child[val];
}
if (type) p->ids.pb(id);
}
void dfs(node *u) {
u->in = ++t;
for (int i = 0; i < 4; ++i) {
if (u->child[i] == nullptr) continue;
dfs(u->child[i]);
}
u->out = t;
for (int val : u->ids) qr[t].pb(val);
u->ids.clear();
}
void dfss() {
dfs(root);
}
pii get(const string x) {
node *p = root;
for (char ch : x) {
int val = sad(ch);
if (p->child[val] == nullptr) return make_pair(-1, -1);
p = p->child[val];
}
return make_pair(p->in, p->out);
}
} pre, suf;
struct BIT {
int bit[N];
void add(int idx, int delta) {
for (; idx <= suf.t; idx += idx & -idx) bit[idx] += delta;
}
int get(int idx) {
int ans = 0;
for (; idx > 0; idx -= idx & -idx) ans += bit[idx];
return ans;
}
int get(int l, int r) {
return get(r) - get(l - 1);
}
} bit;
int res[N], pos[N];
struct node {
int l, r, id;
bool type;
node(int l_, int r_, int id_, bool type_) : l(l_), r(r_), id(id_), type(type_) {}
};
vector<node> sw[N];
inline void solve() {
pre.dfss();
suf.dfss();
for (int i = 1; i <= n; ++i) pos[i] = suf.get(s[i]).fi;
for (int i = 1; i <= q; ++i) {
string a, b;
cin >> a >> b;
auto[l, r] = pre.get(a);
if (l == -1 || r == -1) continue;
reverse(vall(b));
auto[u, v] = suf.get(b);
if (u == -1 || v == -1) continue;
sw[l - 1].pb(node(u, v, i, 0));
sw[r].pb(node(u, v, i, 1));
}
for (int i = 1; i <= pre.t; ++i) {
for (int val : qr[i]) bit.add(pos[val], 1);
for (auto[l, r, id, type] : sw[i]) res[id] = bit.get(l, r) * (type ? 1 : -1);
}
for (int i = 1; i <= q; ++i) cout << res[i] << endl;
}
inline void input() {
cin >> n >> q;
for (int i = 1; i <= n; ++i) {
cin >> s[i];
pre.add(s[i], i, 1);
reverse(vall(s[i]));
suf.add(s[i], i, 0);
}
return solve();
}
bool END;
int main() {
if(fopen(task ".inp", "r")) {
freopen(task ".inp", "r", stdin);
freopen(task ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
input();
cerr << endl << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << 's' << endl;
cerr << "Memory: " << fabs ((&END - &START)) / 1048576.0 << "MB\n";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:158:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
158 | freopen(task ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:159:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
159 | freopen(task ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |