#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
template <class X, class Y> bool minimize(X &a, Y b) {
if (a > b) return a = b, true;
return false;
}
template <class X, class Y> bool maximize(X &a, Y b) {
if (a < b) return a = b, true;
return false;
}
const char base[] = {'A', 'G', 'C', 'U'};
const int N = 2e6 + 7;
int node[N][4];
int cnt[N];
int ind[26];
int f[N];
signed main() {
cin.tie(0)->sync_with_stdio(0);
auto add = [&](string &s) {
static int num_node = 0;
int cur = 0;
for (int i = 0; i < (int) s.size(); i ++) {
int c = ind[s[i] - 'A'];
if (node[cur][c] == 0) {
node[cur][c] = ++ num_node;
}
cur = node[cur][c];
}
cnt[cur] ++;
};
int res = 0;
function<void(string&, string&, int, int, int)> dfs = [&](string &p, string &q, int u, int i, int sz) {
if (i < (int) p.size()) {
if (node[u][ind[p[i] - 'A']] == 0) {
res = 0;
return;
}
int j = f[sz];
while (j > 0 && q[j] != p[i]) {
j = f[j - 1];
}
if (q[j] == p[i]) {
j ++;
}
f[sz + 1] = j;
dfs(p, q, node[u][ind[p[i] - 'A']], i + 1, sz + 1);
} else {
if (f[sz] >= (int) q.size() - 1) {
res += cnt[u];
}
for (char c: base) if (node[u][ind[c - 'A']]) {
int j = f[sz];
while (j > 0 && q[j] != c) {
j = f[j - 1];
}
if (q[j] == c) {
j ++;
}
f[sz + 1] = j;
dfs(p, q, node[u][ind[c - 'A']], i, sz + 1);
}
}
};
for (int i = 0; i < 4; i ++) {
ind[base[i] - 'A'] = i;
}
int n, m; cin >> n >> m;
for (int i = 1; i <= n; i ++) {
string s; cin >> s;
add(s);
}
while (m --) {
string p, q; cin >> p >> q;
q += '#';
res = 0;
for (int i = 1, j = 0; i < (int) q.size(); i ++) {
if (j > 0 && q[j] != q[i]) {
j = f[j - 1];
}
if (q[j] == q[i]) {
j ++;
}
f[i] = j;
}
dfs(p, q, 0, 0, (int) q.size() - 1);
cout << res << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
399 ms |
2900 KB |
Output is correct |
2 |
Correct |
781 ms |
2832 KB |
Output is correct |
3 |
Execution timed out |
1580 ms |
38956 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
1052 KB |
Output is correct |
2 |
Incorrect |
317 ms |
696 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |