// IOI 2021
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) { cerr << ends << H; debug_out(T...); }
#define debug(...) cerr << "{" << #__VA_ARGS__ << "}:", debug_out(__VA_ARGS__)
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
////////////////////////////////////////////////////////////////////
const int N = 1e5 + 5, M = 2e6 + 5, SGM = 4;
int t, clk, CNT[M], C[M][SGM], ST[M], FN[M], Z[N], ANS[N];
string S[N], PRE[N], SUF[N];
vector<pii> Q[N], T;
int Add(string &s) {
int now = 0;
for (char c : s) {
if (!C[now][c - 'A']) C[now][c - 'A'] = ++t;
now = C[now][c - 'A'];
CNT[now]++;
}
return now;
}
void Compress(string &s) {
for (char &c : s) {
if (c == 'G') c = 'B';
if (c == 'U') c = 'D';
}
}
void DFS(int v) {
ST[v] = clk++;
for (int i = 0; i < SGM; i++) if (C[v][i]) DFS(C[v][i]);
FN[v] = clk;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
mt19937 Rnd(time(0));
int n, q; cin >> n >> q;
for (int i = 0; i < n; i++) {
cin >> S[i]; Compress(S[i]);
Z[i] = Add(S[i]);
}
DFS(0);
for (int i = 0; i < n; i++) T.push_back(make_pair(ST[Z[i]], i));
sort(all(T));
for (int i = 0; i < q; i++) {
cin >> PRE[i] >> SUF[i]; Compress(PRE[i]), Compress(SUF[i]);
int now = 0;
for (char c : PRE[i]) {
now = C[now][c - 'A'];
if (!now) break;
}
if (now == 0) continue;
reverse(all(SUF[i]));
int r = lower_bound(all(T), make_pair(ST[now], 0)) - T.begin();
int l = lower_bound(all(T), make_pair(FN[now], 0)) - T.begin();
Q[r].push_back(make_pair(-1, i));
Q[l].push_back(make_pair(+1, i));
}
memset(C, 0, sizeof C), memset(CNT, 0, sizeof CNT), t = 0;
for (int i = 0; i < n; i++) reverse(all(S[i]));
for (int i = 0; i <= n; i++) {
for (pii p : Q[i]) {
int now = 0;
for (char c : SUF[p.second]) {
now = C[now][c - 'A'];
if (!now) break;
}
if (now == 0) continue;
ANS[p.second] += p.first * CNT[now];
}
if (i < n) Add(S[T[i].second]);
}
for (int i = 0; i < q; i++) cout << ANS[i] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
51200 KB |
Output is correct |
2 |
Correct |
31 ms |
51200 KB |
Output is correct |
3 |
Correct |
36 ms |
51200 KB |
Output is correct |
4 |
Correct |
33 ms |
51320 KB |
Output is correct |
5 |
Correct |
37 ms |
51328 KB |
Output is correct |
6 |
Correct |
31 ms |
51200 KB |
Output is correct |
7 |
Correct |
31 ms |
51328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
55672 KB |
Output is correct |
2 |
Correct |
89 ms |
59768 KB |
Output is correct |
3 |
Correct |
134 ms |
74744 KB |
Output is correct |
4 |
Correct |
130 ms |
73976 KB |
Output is correct |
5 |
Correct |
117 ms |
66168 KB |
Output is correct |
6 |
Correct |
130 ms |
66424 KB |
Output is correct |
7 |
Correct |
101 ms |
62588 KB |
Output is correct |
8 |
Correct |
124 ms |
69140 KB |
Output is correct |
9 |
Correct |
115 ms |
68216 KB |
Output is correct |
10 |
Correct |
91 ms |
63096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
53892 KB |
Output is correct |
2 |
Correct |
51 ms |
52728 KB |
Output is correct |
3 |
Correct |
58 ms |
53108 KB |
Output is correct |
4 |
Correct |
56 ms |
52764 KB |
Output is correct |
5 |
Correct |
59 ms |
52600 KB |
Output is correct |
6 |
Correct |
66 ms |
52980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
51200 KB |
Output is correct |
2 |
Correct |
31 ms |
51200 KB |
Output is correct |
3 |
Correct |
36 ms |
51200 KB |
Output is correct |
4 |
Correct |
33 ms |
51320 KB |
Output is correct |
5 |
Correct |
37 ms |
51328 KB |
Output is correct |
6 |
Correct |
31 ms |
51200 KB |
Output is correct |
7 |
Correct |
31 ms |
51328 KB |
Output is correct |
8 |
Correct |
84 ms |
55672 KB |
Output is correct |
9 |
Correct |
89 ms |
59768 KB |
Output is correct |
10 |
Correct |
134 ms |
74744 KB |
Output is correct |
11 |
Correct |
130 ms |
73976 KB |
Output is correct |
12 |
Correct |
117 ms |
66168 KB |
Output is correct |
13 |
Correct |
130 ms |
66424 KB |
Output is correct |
14 |
Correct |
101 ms |
62588 KB |
Output is correct |
15 |
Correct |
124 ms |
69140 KB |
Output is correct |
16 |
Correct |
115 ms |
68216 KB |
Output is correct |
17 |
Correct |
91 ms |
63096 KB |
Output is correct |
18 |
Correct |
59 ms |
53892 KB |
Output is correct |
19 |
Correct |
51 ms |
52728 KB |
Output is correct |
20 |
Correct |
58 ms |
53108 KB |
Output is correct |
21 |
Correct |
56 ms |
52764 KB |
Output is correct |
22 |
Correct |
59 ms |
52600 KB |
Output is correct |
23 |
Correct |
66 ms |
52980 KB |
Output is correct |
24 |
Correct |
100 ms |
60408 KB |
Output is correct |
25 |
Correct |
113 ms |
61816 KB |
Output is correct |
26 |
Correct |
95 ms |
60024 KB |
Output is correct |
27 |
Correct |
157 ms |
72568 KB |
Output is correct |
28 |
Correct |
182 ms |
65256 KB |
Output is correct |
29 |
Correct |
136 ms |
58604 KB |
Output is correct |