#include <iostream>
#include <vector>
#include<math.h>
using namespace std;
int main()
{
int n, k; cin >> n >> k;
string s, l = "JOI"; cin >> s;
vector<vector<int>> pre(3, vector<int>(n, 0));
for (int i = 0; i < 3; ++i) {
int x = n - 1, y = n - 1, cnt = 0, cur = -1;
while (((x >= 0) && (y >= 0))) {
while ((x >= 0) && (cnt < k)) {
if (s[x] == l[i]) cnt++;
if (cnt == k) break;
pre[i][x] = cur; --x;
}
if (x < 0) break;
for (; y >= x; --y) if (s[y] == l[i]) break;
cur = y, y--, cnt--, pre[i][x--] = cur;
}
}
int ans = n + 1;
for (int f = 0; f < n; ++f) {
int l = f + 2, r = n - 1;
while (l <= r) {
int e = (l + r) / 2, j = f;
bool ok = 1;
for (int i = 0; i < 3; ++i) {
j = pre[i][j];
if ((j == -1) || (j > e)) { ok = 0; break; }
}
if (ok) ans = min(ans, e - f + 1), r = e - 1;
else l = e + 1;
}
}
cout << ((ans == (n + 1)) ? -1 : ans - (3 * k)) << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |