#include <iostream>
#include <vector>
using namespace std;
int nxtk(string& s, int i, vector<vector<int>>& pre, int j, int k) {
int ans = j;
for (int x = 0; ((x < k) && (ans != -1)); ++x)
ans = pre[ans][i] + 1;
return ans;
}
int main()
{
int n, k; cin >> n >> k;
string s, l = "JOI"; cin >> s;
vector<vector<int>> pre(n + 1, vector<int>(3, -2));
for (int i = 0; i < 3; ++i)
for (int j = n - 1; j >= 0; --j)
pre[j][i] = ((s[j] == l[i]) ? j : pre[j + 1][i]);
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; j--, ++i) {
j = nxtk(s, i, pre, j, k);
if ((j == -1) || (j > (e + 1))) { 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... |