#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) {
for(int v = 0; v < 4; ++v) {
int z = t[u][v];
if(z < 0) continue;
dfs(z);
if(cnt[z].size() > cnt[u].size()) swap(cnt[u], cnt[z]);
for(auto [x, y] : cnt[z]) cnt[u][x] += y;
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
156828 KB |
Output is correct |
2 |
Correct |
81 ms |
156848 KB |
Output is correct |
3 |
Correct |
77 ms |
156868 KB |
Output is correct |
4 |
Correct |
79 ms |
156840 KB |
Output is correct |
5 |
Correct |
81 ms |
156884 KB |
Output is correct |
6 |
Correct |
79 ms |
156876 KB |
Output is correct |
7 |
Correct |
96 ms |
156844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1342 ms |
333564 KB |
Output is correct |
2 |
Correct |
1284 ms |
345560 KB |
Output is correct |
3 |
Correct |
556 ms |
276496 KB |
Output is correct |
4 |
Correct |
529 ms |
271660 KB |
Output is correct |
5 |
Execution timed out |
1601 ms |
454084 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
158336 KB |
Output is correct |
2 |
Correct |
105 ms |
158604 KB |
Output is correct |
3 |
Correct |
129 ms |
158512 KB |
Output is correct |
4 |
Correct |
105 ms |
157892 KB |
Output is correct |
5 |
Correct |
104 ms |
158348 KB |
Output is correct |
6 |
Correct |
112 ms |
158380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
156828 KB |
Output is correct |
2 |
Correct |
81 ms |
156848 KB |
Output is correct |
3 |
Correct |
77 ms |
156868 KB |
Output is correct |
4 |
Correct |
79 ms |
156840 KB |
Output is correct |
5 |
Correct |
81 ms |
156884 KB |
Output is correct |
6 |
Correct |
79 ms |
156876 KB |
Output is correct |
7 |
Correct |
96 ms |
156844 KB |
Output is correct |
8 |
Correct |
1342 ms |
333564 KB |
Output is correct |
9 |
Correct |
1284 ms |
345560 KB |
Output is correct |
10 |
Correct |
556 ms |
276496 KB |
Output is correct |
11 |
Correct |
529 ms |
271660 KB |
Output is correct |
12 |
Execution timed out |
1601 ms |
454084 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |