#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <unordered_map>
#include <stack>
#include <bitset>
#include <string>
#include <cstring>
#include <iterator>
#include <random>
#define ll long long
#define ld long double
#define inf (ll)(2*1e18)
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define endl "\n"
using namespace std;
void solve(){
ll n, k, i, res=inf, x, y;
string s;
cin>>n>>k>>s;
vector<ll> J;
vector<ll> O;
vector<ll> I;
for(i=0;i<n;++i){
if(s[i] == 'J'){
J.pb(i);
}else if(s[i] == 'O'){
O.pb(i);
}else{
I.pb(i);
}
}
for(i=k-1;i<J.size();++i){
x = lower_bound(O.begin(), O.end(), J[i]) - O.begin();
if(x+k-1 >= O.size()) break;
y = lower_bound(I.begin(), I.end(), O[x+k-1]) - I.begin();
if(y+k-1 >= I.size()) break;
res = min(I[y+k-1] - J[i-k+1] + 1 - 3*k, res);
}
if(res == inf){
cout<<-1<<endl;
return;
}
cout<<res<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
srand(time(nullptr));
ll t=1;
// cin>>t;
for(;t>0;--t){
solve();
}
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... |