#include <bits/stdc++.h>
#define int long long
using namespace std;
ostream &operator <<(ostream &out, vector <int> val) {
for (auto v : val) {
out << v << ' ';
}
return out;
}
void solve() {
int n, k;
cin >> n >> k;
string s;
vector <vector <int>> ind(3);
cin >> s;
for (int i = 0; i < n; ++i) {
if (s[i] == 'J') {
ind[0].push_back(i);
} else if (s[i] == 'O') {
ind[1].push_back(i);
} else {
ind[2].push_back(i);
}
}
int ans = 2e9;
for (int i = 0; i < n; ++i) {
int num = i;
bool flag = true;
for (int j = 0; j < 3; ++j) {
if (ind[j].empty()) {
flag = false;
} else {
int res = lower_bound(ind[j].begin(), ind[j].end(), num) - ind[j].begin();
res += k - 1;
if (res < ind[j].size()) {
num = ind[j][res] + 1;
} else {
flag = false;
}
}
}
if (flag) {
ans = min(ans, num - i - 3 * k);
}
}
if (ans == 2e9) {
cout << - 1 << '\n';
} else {
cout << ans << '\n';
}
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int t = 1;
solve();
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |