#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int n,k; cin>>n>>k;
string s; cin>>s;
vector<int> c(3,0);
for (char i: s) {
if (i=='J') c[0]++;
else if (i=='O') c[1]++;
else c[2]++;
}
if (c[0]<k||c[1]<k||c[2]<k) cout<<-1;
else {
int ans=0;
bool f=false;
int ind=0,c=0,prev=0;
while (ind<n&&c<k) {
if (s[ind]=='J') {
if (!f) {
f=true;
prev=ind;
} else {
ans+=ind-prev-1;
prev=ind;
}
c++;
}
ind++;
}
if (c==k&&ind<n) {
c=0;
f=false;
while (ind<n&&c<k) {
if (s[ind]=='O') {
if (!f) {
f=true;
prev=ind;
} else {
ans+=ind-prev-1;
prev=ind;
}
c++;
}
ind++;
}
if (c==k&&ind<n) {
c=0;
f=false;
while (ind<n&&c<k) {
if (s[ind]=='I') {
if (!f) {
f=true;
prev=ind;
} else {
ans+=ind-prev-1;
prev=ind;
}
c++;
}
ind++;
}
if (c==k) cout<<ans;
else cout<<-1;
} else cout<<-1;
} else cout<<-1;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
428 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
428 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
428 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |