#include <bits/stdc++.h>
#define int long long
#define ff first
#define ss second
#define pb push_back
using namespace std;
const int N = 2e5+10;
const int INF = 1e18;
int po[N];
int so[N];
int si[N];
int pj[N];
int n,k;
string s;
void polve(){
cin>>n>>k;
cin>>s;
if(s[n-1] == 'I') si[n-1] = 1;
for(int i = n-2;i>=0;i--){
if(s[i] == 'I') si[i] = si[i+1]+1;
else si[i] = si[i+1];
}
if(s[0] == 'J') pj[0] = 1;
for(int i = 1;i<n;i++){
if(s[i] == 'J') pj[i] = pj[i-1]+1;
else pj[i] = pj[i-1];
}
for(int i = 1;i<n;i++){
if(s[i] == 'O' && pj[i]>=k) po[i] = po[i-1]+1;
else po[i] = po[i-1];
}
for(int i = n-1;i>=0;i--){
if(s[i] == 'O' && si[i]>=k) so[i] = so[i+1]+1;
else so[i] = so[i+1];
}
bool ok = 0;
bool ok1 = 0;
for(int i=0;i<n;i++){
if(po[i] != 0) ok = 1;
if(so[i] != 0) ok1 = 1;
}
if(!ok || !ok1){
cout<<-1;
return;
}
// for(int i = 0;i<n;i++){
// cout<<pj[i]<<' ';
// }
// cout<<'\n';
// for(int i = 0;i<n;i++){
// cout<<si[i]<<' ';
// }
// cout<<'\n';
// for(int i = 0;i<n;i++){
// cout<<po[i]<<' ';
// }
// cout<<'\n';
// for(int i = 0;i<n;i++){
// cout<<so[i]<<' ';
// }
// cout<<'\n';
int pos1 = -1;
int pos2 = -1;
for(int i= 0;i<n;i++){
if(so[i] == 0 || po[i] == k){
int x = si[i]-k+1;
for(int j = i;j<n;j++){
if(si[j] == x && s[j] == 'I'){
pos2 = j;
break;
}
}
break;
}
}
for(int i= n-1;i>=0;i--){
if(po[i] == 0 || so[i] == k){
int x = pj[i]-k+1;
for(int j = i;j>=0;j--){
if(pj[j] == x && s[j] == 'J'){
pos1 = j;
break;
}
}
break;
}
}
cout<<pos2-pos1+1-k*3;
}
signed main(){
// freopen("herding.in","r",stdin);
// freopen("herding.out","w",stdout);
int t = 1;
// cin>>t;
for(int i = 1;i<=t;i++){
// cout<<"Case "<<i<<":\n";
polve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |