이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=2e5+7;
int nxt[LIM][3];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n, k;
string s;
cin >> n >> k >> s;
int a=0, b=0, c=0;
for(auto i : s) {
if(a<k) {
if(i=='J') ++a;
} else if(b<k) {
if(i=='O') ++b;
} else {
if(i=='I') ++c;
}
}
if(c<k) {
cout << -1 << '\n';
return 0;
}
string x="JOI";
rep(i, 3) {
int l=-1, ile=0;
rep(j, n) {
while(l<n-1 && ile<k) {
++l;
if(s[l]==x[i]) ++ile;
}
if(ile==k) nxt[j][i]=l;
else nxt[j][i]=-1;
if(s[j]==x[i]) --ile;
}
}
int ans=n;
rep(i, n) {
int a=nxt[i][0];
if(a!=-1 && a<n-1) {
a=nxt[a+1][1];
if(a!=-1 && a<n-1) {
a=nxt[a+1][2];
if(a!=-1) {
ans=min(ans, a-i+1-3*k);
}
}
}
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |