#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb emplace_back
#define T pair<int,int>
#define SZ(val) (int)val.size()
#define all(val) val.begin(), val.end()
#define db(val) "[" #val " = " << (val) << "] "
const int maxn = 1e5 + 4;
struct Node {
int nxt[4], id, cnt, leaf;
Node() {
memset(nxt, 0, sizeof nxt);
id = cnt = leaf = 0;
}
};
int n, q, num, res[maxn];
string a[maxn];
map<char,char> mp;
vector<Node> t(1);
vector<tuple<string,int,int>> que[maxn];
void add(string &s, int id = 0) {
int v = 0;
for (int i = 0; i < SZ(s); i++) {
int c = s[i] - 'a';
if (!t[v].nxt[c]) {
t[v].nxt[c] = t.size();
t.pb();
}
v = t[v].nxt[c];
if (!t[v].id) t[v].id = id;
t[v].cnt++;
}
t[v].leaf++;
}
T Find(string &s) {
int v = 0;
for (int i = 0; i < SZ(s); i++) {
int c = s[i] - 'a';
if (!t[v].nxt[c]) return {1, 0};
v = t[v].nxt[c];
}
return {t[v].id, t[v].cnt};
}
void dfs(int v, string &s) {
while (t[v].leaf--) a[++num] = s;
for (int c = 0; c < 4; c++)
if (t[v].nxt[c]) {
s += char(c + 'a');
dfs(t[v].nxt[c], s);
s.pop_back();
}
}
string Convert(string &s) {
string t;
for (int i = 0; i < SZ(s); i++)
t += mp[s[i]];
return t;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> q;
mp['A'] = 'a', mp['U'] = 'b', mp['G'] = 'c', mp['C'] = 'd';
for (int i = 1; i <= n; i++) {
string s; cin >> s;
s = Convert(s);
add(s);
}
string s;
dfs(0, s);
vector<Node>(1).swap(t);
for (int i = 1; i <= n; i++)
add(a[i], i);
for (int i = 1; i <= q; i++) {
string u, v; cin >> u >> v;
u = Convert(u);
v = Convert(v);
T pos = Find(u);
que[pos.fi - 1].pb(v, i, -1);
que[pos.fi + pos.se - 1].pb(v, i, 1);
}
vector<Node>(1).swap(t);
for (int i = 1; i <= n; i++) {
reverse(all(a[i])); add(a[i]);
for (auto it : que[i]) {
string s; int id, t; tie(s, id, t) = it;
reverse(all(s));
res[id] += t * Find(s).se;
}
}
for (int i = 1; i <= q; i++)
cout << res[i] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5724 KB |
Output is correct |
2 |
Correct |
2 ms |
5724 KB |
Output is correct |
3 |
Correct |
2 ms |
5724 KB |
Output is correct |
4 |
Correct |
2 ms |
5724 KB |
Output is correct |
5 |
Correct |
2 ms |
5724 KB |
Output is correct |
6 |
Correct |
2 ms |
5724 KB |
Output is correct |
7 |
Correct |
2 ms |
5724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
71856 KB |
Output is correct |
2 |
Correct |
86 ms |
70520 KB |
Output is correct |
3 |
Correct |
150 ms |
98224 KB |
Output is correct |
4 |
Correct |
145 ms |
96428 KB |
Output is correct |
5 |
Correct |
146 ms |
98368 KB |
Output is correct |
6 |
Correct |
151 ms |
98888 KB |
Output is correct |
7 |
Correct |
74 ms |
17580 KB |
Output is correct |
8 |
Correct |
123 ms |
68400 KB |
Output is correct |
9 |
Correct |
121 ms |
63788 KB |
Output is correct |
10 |
Correct |
86 ms |
59580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
11188 KB |
Output is correct |
2 |
Correct |
17 ms |
10028 KB |
Output is correct |
3 |
Correct |
20 ms |
10652 KB |
Output is correct |
4 |
Correct |
17 ms |
9620 KB |
Output is correct |
5 |
Correct |
18 ms |
9492 KB |
Output is correct |
6 |
Correct |
20 ms |
10308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5724 KB |
Output is correct |
2 |
Correct |
2 ms |
5724 KB |
Output is correct |
3 |
Correct |
2 ms |
5724 KB |
Output is correct |
4 |
Correct |
2 ms |
5724 KB |
Output is correct |
5 |
Correct |
2 ms |
5724 KB |
Output is correct |
6 |
Correct |
2 ms |
5724 KB |
Output is correct |
7 |
Correct |
2 ms |
5724 KB |
Output is correct |
8 |
Correct |
85 ms |
71856 KB |
Output is correct |
9 |
Correct |
86 ms |
70520 KB |
Output is correct |
10 |
Correct |
150 ms |
98224 KB |
Output is correct |
11 |
Correct |
145 ms |
96428 KB |
Output is correct |
12 |
Correct |
146 ms |
98368 KB |
Output is correct |
13 |
Correct |
151 ms |
98888 KB |
Output is correct |
14 |
Correct |
74 ms |
17580 KB |
Output is correct |
15 |
Correct |
123 ms |
68400 KB |
Output is correct |
16 |
Correct |
121 ms |
63788 KB |
Output is correct |
17 |
Correct |
86 ms |
59580 KB |
Output is correct |
18 |
Correct |
23 ms |
11188 KB |
Output is correct |
19 |
Correct |
17 ms |
10028 KB |
Output is correct |
20 |
Correct |
20 ms |
10652 KB |
Output is correct |
21 |
Correct |
17 ms |
9620 KB |
Output is correct |
22 |
Correct |
18 ms |
9492 KB |
Output is correct |
23 |
Correct |
20 ms |
10308 KB |
Output is correct |
24 |
Correct |
92 ms |
73264 KB |
Output is correct |
25 |
Correct |
103 ms |
77712 KB |
Output is correct |
26 |
Correct |
90 ms |
73164 KB |
Output is correct |
27 |
Correct |
158 ms |
97228 KB |
Output is correct |
28 |
Correct |
113 ms |
33488 KB |
Output is correct |
29 |
Correct |
73 ms |
19276 KB |
Output is correct |