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