#include "bits/stdc++.h"
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using i32 = int;
using u32 = unsigned;
using i16 = short;
using u16 = unsigned short;
using ld = long double;
using ii = pair<int, int>;
const int N = 2e6, mod = 1e9 + 9, base = 1845;
vector<int> h(string& s) {
int n = s.size();
vector<int> a(n + 1);
i64 k = 1;
a[0] = 0;
for(int i = 1; i <= n; ++i) {
int x0 = a[i - 1];
int& x1 = a[i];
k = k * base % mod;
x1 = (x0 + (s[i - 1] - '0' + 1) * k) % mod;
}
a.erase(a.begin());
return a;
}
int t[N][4], nos = 1;
unordered_map<int, int> cnt[N];
vector<ii> query[N];
vector<int> ans;
void push(string& s) {
int i = 0;
for(char ch : s) {
int& k = t[i][ch - '0'];
if(k < 0) {
memset(t[nos], -1, sizeof(t[nos]));
k = nos++;
}
i = k;
}
reverse(s.begin(), s.end());
for(int y : h(s)) ++cnt[i][y];
}
void f(string& s) {
string alpha = "AGCU";
for(char& ch : s) {
for(int i = 0; i < 4; ++i) {
if(ch == alpha[i]) {
ch = i + '0';
break;
}
}
}
}
void dfs(int u, bool q = false) {
q = q || !query[u].empty();
for(int v = 0; v < 4; ++v) {
int z = t[u][v];
if(z < 0) continue;
dfs(z, q);
if(!q) continue;
if(cnt[z].size() > cnt[u].size()) swap(cnt[u], cnt[z]);
for(auto [x, y] : cnt[z]) cnt[u][x] += y;
}
if(!q) return;
for(auto [pos, ht] : query[u]) {
auto it = cnt[u].find(ht);
if(it == cnt[u].end()) continue;
ans[pos] += it->second;
}
}
void solve() {
int n, m;
cin >> n >> m;
memset(t[0], -1, sizeof(t[0]));
ans.assign(m, 0);
for(int i = 0; i < n; ++i) {
string s;
cin >> s;
f(s);
push(s);
}
for(int i = 0; i < m; ++i) {
string a, b;
int k = 0;
cin >> a >> b;
f(a);
f(b);
reverse(b.begin(), b.end());
for(char ch : a) {
if(t[k][ch - '0'] < 0) {
k = -1;
break;
}
k = t[k][ch - '0'];
}
if(k < 0) continue;
query[k].emplace_back(i, h(b).back());
}
dfs(0);
for(int k : ans) cout << k << '\n';
}
int main() {
ios_base :: sync_with_stdio(false);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
156784 KB |
Output is correct |
2 |
Correct |
80 ms |
156976 KB |
Output is correct |
3 |
Correct |
76 ms |
156856 KB |
Output is correct |
4 |
Correct |
75 ms |
156844 KB |
Output is correct |
5 |
Correct |
75 ms |
156896 KB |
Output is correct |
6 |
Correct |
76 ms |
156856 KB |
Output is correct |
7 |
Correct |
77 ms |
156808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1286 ms |
334404 KB |
Output is correct |
2 |
Correct |
1231 ms |
346084 KB |
Output is correct |
3 |
Correct |
504 ms |
272948 KB |
Output is correct |
4 |
Correct |
485 ms |
268056 KB |
Output is correct |
5 |
Correct |
1160 ms |
414924 KB |
Output is correct |
6 |
Correct |
1160 ms |
421868 KB |
Output is correct |
7 |
Correct |
333 ms |
227524 KB |
Output is correct |
8 |
Correct |
349 ms |
255492 KB |
Output is correct |
9 |
Correct |
347 ms |
249152 KB |
Output is correct |
10 |
Correct |
807 ms |
322928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
106 ms |
157892 KB |
Output is correct |
2 |
Correct |
105 ms |
158332 KB |
Output is correct |
3 |
Correct |
105 ms |
158076 KB |
Output is correct |
4 |
Correct |
100 ms |
157380 KB |
Output is correct |
5 |
Correct |
102 ms |
158004 KB |
Output is correct |
6 |
Correct |
112 ms |
158188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
156784 KB |
Output is correct |
2 |
Correct |
80 ms |
156976 KB |
Output is correct |
3 |
Correct |
76 ms |
156856 KB |
Output is correct |
4 |
Correct |
75 ms |
156844 KB |
Output is correct |
5 |
Correct |
75 ms |
156896 KB |
Output is correct |
6 |
Correct |
76 ms |
156856 KB |
Output is correct |
7 |
Correct |
77 ms |
156808 KB |
Output is correct |
8 |
Correct |
1286 ms |
334404 KB |
Output is correct |
9 |
Correct |
1231 ms |
346084 KB |
Output is correct |
10 |
Correct |
504 ms |
272948 KB |
Output is correct |
11 |
Correct |
485 ms |
268056 KB |
Output is correct |
12 |
Correct |
1160 ms |
414924 KB |
Output is correct |
13 |
Correct |
1160 ms |
421868 KB |
Output is correct |
14 |
Correct |
333 ms |
227524 KB |
Output is correct |
15 |
Correct |
349 ms |
255492 KB |
Output is correct |
16 |
Correct |
347 ms |
249152 KB |
Output is correct |
17 |
Correct |
807 ms |
322928 KB |
Output is correct |
18 |
Correct |
106 ms |
157892 KB |
Output is correct |
19 |
Correct |
105 ms |
158332 KB |
Output is correct |
20 |
Correct |
105 ms |
158076 KB |
Output is correct |
21 |
Correct |
100 ms |
157380 KB |
Output is correct |
22 |
Correct |
102 ms |
158004 KB |
Output is correct |
23 |
Correct |
112 ms |
158188 KB |
Output is correct |
24 |
Correct |
1240 ms |
356364 KB |
Output is correct |
25 |
Correct |
1192 ms |
356124 KB |
Output is correct |
26 |
Correct |
1199 ms |
355324 KB |
Output is correct |
27 |
Correct |
499 ms |
257636 KB |
Output is correct |
28 |
Correct |
336 ms |
198336 KB |
Output is correct |
29 |
Correct |
166 ms |
161988 KB |
Output is correct |