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 N = 2e5;
const string JOI = "JOI";
int n, k;
string s;
int jmp[3][N + 1];
int solve(int i) {
i = jmp[0][i];
i = jmp[1][i];
i = jmp[2][i];
return i == n ? -1 : i;
}
int main() {
cin.tie(0)->sync_with_stdio(0), cin.exceptions(cin.failbit);
cin >> n >> k >> s;
for (int x = 0; x < 3; x++) {
jmp[x][n] = n;
for (int i = 0, j = 0, c = 0; i < n; i++) {
while (j < n && c < k) c += s[j++] == JOI[x];
jmp[x][i] = c == k ? j - 1 : n;
c -= s[i] == JOI[x];
}
}
int ans = n;
for (int i = 0; i < n; i++) {
int j = solve(i);
if (j != -1) ans = min(ans, j - i + 1 - k * 3);
}
cout << (ans == n ? -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... |