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"
using namespace std;
#define ar array
const int N = 2e5 + 5;
int dp[3][N];
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n, k; cin>>n>>k;
string s; cin>>s;
vector<int> a(n);
for(int i=0;i<n;i++){
if(s[i] == 'J') a[i] = 0;
if(s[i] == 'O') a[i] = 1;
if(s[i] == 'I') a[i] = 2;
}
memset(dp, -1, sizeof dp);
for(int j=0;j<3;j++){
vector<int> p;
for(int i=0;i<n;i++){
if(a[i] == j) p.push_back(i);
} p.push_back(n);
for(int l=k-1;l+1<(int)p.size();l++){
for(int i=p[l];i<p[l+1];i++) dp[j][i] = p[l-k+1];
}
}
int res = 1e9;
for(int i=0;i<n;i++){
int l = i;
for(int j=2;~j && l >= 0;j--){
l = dp[j][l];
if(j) l--;
}
if(l >= 0){
res = min(res, i - l + 1);
}
}
if(res == 1e9) cout<<-1<<"\n";
else cout<<res - 3 * k<<"\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... |