#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O2")
#pragma GCC target("avx,avx2,fma")
#pragma GCC target("popcnt")
int N = 2*1e6+1;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n, m;
cin >> n >> m;
vector<array<int, 4>> prefix(N, {-1, -1, -1, -1});
vector<array<int, 4>> suffix(N, {-1, -1, -1, -1});
vector<int> preind(N, -1);
vector<int> sufind(N, -1);
vector<int> alph(100);
alph['A'] = 0;
alph['C'] = 1;
alph['G'] = 2;
alph['U'] = 3;
vector<string> ns(n);
vector<int> pind(m);
vector<int> sind(m);
int cp = 0;
int cs = 0;
int dp = 0;
int ds = 0;
for (int i = 0; i < n; i++) cin >> ns[i];
for (int i = 0; i < m; i++)
{
string a, b;
cin >> a >> b;
reverse(b.begin(), b.end());
int curr = 0;
for (char c:a)
{
int next = alph[c];
if (prefix[curr][next]==-1)
{
prefix[curr][next] = ++cp;
}
curr = prefix[curr][next];
}
if (preind[curr]==-1)
{
preind[curr] = ++dp;
pind[i] = dp;
}
else pind[i] = preind[curr];
curr = 0;
for (char c:b)
{
int next = alph[c];
if (suffix[curr][next]==-1)
{
suffix[curr][next] = ++cs;
}
curr = suffix[curr][next];
}
if (sufind[curr] ==-1)
{
sufind[curr] = ++ds;
sind[i] = ds;
}
else sind[i] =sufind[curr];
}
vector<bitset<100000>> pnodes(dp+1);
vector<bitset<100000>> snodes(ds+1);
vector<vector<int>> p2nodes(N);
vector<vector<int>> s2nodes(N);
for (int i = 0; i < n; i++)
{
int curr = 0;
for (char c:ns[i])
{
int next = alph[c];
curr = prefix[curr][next];
if (curr == -1) break;
if (preind[curr]!=-1) pnodes[preind[curr]][i] = 1;
}
reverse(ns[i].begin(), ns[i].end());
curr = 0;
for (char c:ns[i])
{
int next = alph[c];
curr = suffix[curr][next];
if (curr == -1) break;
if (sufind[curr]!=-1) snodes[sufind[curr]][i] = 1;
}
}
for (int i = 0; i < m; i++)
{
cout << (snodes[sind[i]]&pnodes[pind[i]]).count() << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
173648 KB |
Output is correct |
2 |
Correct |
35 ms |
173660 KB |
Output is correct |
3 |
Correct |
37 ms |
173552 KB |
Output is correct |
4 |
Correct |
41 ms |
173544 KB |
Output is correct |
5 |
Correct |
35 ms |
173660 KB |
Output is correct |
6 |
Correct |
36 ms |
173488 KB |
Output is correct |
7 |
Correct |
36 ms |
173396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
191836 KB |
Output is correct |
2 |
Correct |
71 ms |
188984 KB |
Output is correct |
3 |
Correct |
72 ms |
188752 KB |
Output is correct |
4 |
Correct |
75 ms |
191320 KB |
Output is correct |
5 |
Correct |
107 ms |
294736 KB |
Output is correct |
6 |
Correct |
112 ms |
294740 KB |
Output is correct |
7 |
Correct |
98 ms |
211532 KB |
Output is correct |
8 |
Correct |
110 ms |
254288 KB |
Output is correct |
9 |
Correct |
117 ms |
271184 KB |
Output is correct |
10 |
Correct |
64 ms |
179276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
175012 KB |
Output is correct |
2 |
Correct |
91 ms |
175256 KB |
Output is correct |
3 |
Correct |
111 ms |
175696 KB |
Output is correct |
4 |
Correct |
97 ms |
174584 KB |
Output is correct |
5 |
Correct |
97 ms |
175696 KB |
Output is correct |
6 |
Correct |
115 ms |
175808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
173648 KB |
Output is correct |
2 |
Correct |
35 ms |
173660 KB |
Output is correct |
3 |
Correct |
37 ms |
173552 KB |
Output is correct |
4 |
Correct |
41 ms |
173544 KB |
Output is correct |
5 |
Correct |
35 ms |
173660 KB |
Output is correct |
6 |
Correct |
36 ms |
173488 KB |
Output is correct |
7 |
Correct |
36 ms |
173396 KB |
Output is correct |
8 |
Correct |
64 ms |
191836 KB |
Output is correct |
9 |
Correct |
71 ms |
188984 KB |
Output is correct |
10 |
Correct |
72 ms |
188752 KB |
Output is correct |
11 |
Correct |
75 ms |
191320 KB |
Output is correct |
12 |
Correct |
107 ms |
294736 KB |
Output is correct |
13 |
Correct |
112 ms |
294740 KB |
Output is correct |
14 |
Correct |
98 ms |
211532 KB |
Output is correct |
15 |
Correct |
110 ms |
254288 KB |
Output is correct |
16 |
Correct |
117 ms |
271184 KB |
Output is correct |
17 |
Correct |
64 ms |
179276 KB |
Output is correct |
18 |
Correct |
129 ms |
175012 KB |
Output is correct |
19 |
Correct |
91 ms |
175256 KB |
Output is correct |
20 |
Correct |
111 ms |
175696 KB |
Output is correct |
21 |
Correct |
97 ms |
174584 KB |
Output is correct |
22 |
Correct |
97 ms |
175696 KB |
Output is correct |
23 |
Correct |
115 ms |
175808 KB |
Output is correct |
24 |
Correct |
97 ms |
183380 KB |
Output is correct |
25 |
Correct |
148 ms |
181636 KB |
Output is correct |
26 |
Correct |
86 ms |
186964 KB |
Output is correct |
27 |
Correct |
97 ms |
183376 KB |
Output is correct |
28 |
Correct |
248 ms |
184880 KB |
Output is correct |
29 |
Correct |
238 ms |
181840 KB |
Output is correct |