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 ll long long
#define str string
#define ins insert
#define ld long double
#define pb push_back
#define pf push_front
#define pof pop_front()
#define pob pop_back()
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz size()
#define vll vector<ll>
#define bc back()
#define arr array
#define pll vector<pair<ll,ll>>
using namespace std;
template <class _T>
bool chmin(_T &x, const _T &y){
bool f=0;
if (x>y){x=y;f=1;}
return f;
}
template <class _T>
bool chmax(_T &x, const _T &y){
bool f=0;
if (x<y){x=y;f=1;}
return f;
}
//void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
void start(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const ll inf=2e18+7;
const ll mod=1e9+7;
const ll N=1e5+7;
const ld eps=1e-9;
void solve(){
ll i,j;
ll n,k;
cin>>n>>k;
str s;
cin>>s;
ll JOI[n][3];
str s2;
s2="JOI";
for(i=0;i<3;i++){
ll l=0,r=0;
ll c=0;
if(s[0]==s2[i])c++;
while(l<n){
while(c<k && r<n){
r++;
if(r==n) break;
if(s[r]==s2[i])c++;
}
if(c<k){
JOI[l][i]=inf;
l++;
continue;
}
JOI[l][i]=r;
if(s[l]==s2[i])c--;
l++;
}
}
ll ans=inf;
for(i=0;i<n;i++){
ll l=i,r=JOI[i][0];
for(j=1;j<3;j++){
if(r==inf)continue;
r=JOI[r][j];
}
ans=min(ans,r-l+1-3*k);
}
if(ans>n)cout<<-1<<endl;
else cout<<ans<<endl;
}
signed main(){
//start();
ll t=1;
//cin>>t;
while(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... |