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>
#define finish(x) return cout << x << endl, 0
typedef long long ll;
typedef long double ldb;
const int md = 1e9 + 7;
const ll inf = 4e18;
const int OO = 1;
const int OOO = 1;
using namespace std;
const string word = "JOI";
int n, k;
string s;
vector<int> nxt[3];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> k >> s;
for (int i = 0; i < 3; i++) {
char need = word[i];
nxt[i].resize(n, md);
int p = 0, cnt = 0;
while (p < n && cnt < k) {
if (s[p] == need) cnt++;
p++;
}
for (int j = 0; j < n; j++) {
nxt[i][j] = p;
if (s[j] == need) {
if (p == n) break;
while (p < n && s[p++] != need);
if (p == n && s.back() != need) break;
}
}
}
int ans = md;
for (int i = 0; i < n; i++) {
int cur = i;
for (int j = 0; j < 3 && cur <= n; j++)
if (cur == n)
cur = md;
else
cur = nxt[j][cur];
if (cur <= n) ans = min(ans, cur - i - 3 * k);
}
cout << (ans == md ? -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... |