This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int MXN = 2e5 + 5, LG = __lg(MXN) + 1;
int jmp[3][LG][MXN];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
string s;
cin >> n >> m >> s;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < LG; j++) {
for (int k = 0; k <= n + 1; k++) {
jmp[i][j][k] = n + 1;
}
}
}
for (int i = 0; i < 3; i++) {
for (int j = n - 1; j >= 0; j--) {
jmp[i][0][j] = (s[j] == "JOI"[i] ? j + 1 : jmp[i][0][j + 1]);
}
for (int j = 1; j < LG; j++) {
for (int k = 0; k + (1 << j) <= n; k++) {
jmp[i][j][k] = jmp[i][j - 1][jmp[i][j - 1][k]];
}
}
}
int ans = 1e9;
for (int i = 0; i < n; i++) {
int idx = i;
for (int j = 0; j < 3; j++) {
for (int k = 0; k < LG; k++) {
if ((m >> k) & 1) {
idx = jmp[j][k][idx];
}
}
}
if (idx != n + 1) {
ans = min(ans, idx - i - 3 * m);
}
}
cout << (ans == 1e9 ? -1 : ans) << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |