이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |