#include <bits/stdc++.h>
#define int long long
using namespace std;
const long long N = 2e5 + 5;
int a[N];
int n, k;
int p1[N];
int p2[N];
int p3[N];
void solve()
{
int n, k;
string s;
cin >> n >> k >> s;
for (int i = 1; i <= n; i++)
{
p1[i] = p1[i - 1];
p2[i] = p2[i - 1];
p3[i] = p3[i - 1];
if (s[i - 1] == 'J')
p1[i]++;
else if (s[i - 1] == 'O')
p2[i]++;
else
p3[i]++;
}
int ans = 1e18;
for (int i = 0; i <= n; i++)
{
int next = lower_bound(p1, p1 + n + 1, p1[i] + k) - p1;
if (next > n)
continue;
int J = next;
next = lower_bound(p2, p2 + n + 1, p2[J] + k) - p2;
if (next > n)
continue;
int O = next;
next = lower_bound(p3, p3 + n + 1, p3[O] + k) - p3;
if (next > n)
continue;
int I = next;
if (p1[J] - p1[i] >= k and p2[O] - p2[J] >= k and p3[I] - p3[O] >= k)
ans = min(ans, I - i);
}
cout << ((ans == 1e18) ? -1 : ans - 3 * k);
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
for (int i = 1; i <= t; i++)
{
solve();
cout << endl;
}
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |