#include "bits/stdc++.h"
using namespace std;
const int N=2e5;
const string JOI="JOI";
int n,k;
string s;
int nxt[N+1][3];
int test(int i) {
for(int x=0;x<3;x++)for(int j=0;j<k;j++){
i=nxt[i][x];
if(i==n)return -1;
i++;
}
return i;
}
int main() {
cin.tie(0)->sync_with_stdio(0),cin.exceptions(cin.failbit);
cin>>n>>k>>s;
nxt[n][0]=nxt[n][1]=nxt[n][2]=n;
for(int i=n-1;i>=0;i--)for(int j=0;j<3;j++)nxt[i][j]=s[i]==JOI[j]?i:nxt[i+1][j];
if(test(0)==-1){
cout<<-1<<'\n';
return 0;
}
int low=0,hi=n-1;
while(low<hi){
int mid=(low+hi)/2+1;
if(test(mid)!=-1)low=mid;
else hi=mid-1;
}
cout<<(test(low)-low-k*3)<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |