Submission #675345

#TimeUsernameProblemLanguageResultExecution timeMemory
675345sudheerays123JJOOII 2 (JOI20_ho_t2)C++17
13 / 100
2091 ms5532 KiB
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long int
const ll N = 100+5 , INF = 1e18 , MOD = 1e9+7;

void solve(){

	ll n,k;
	string s;

	cin >> n >> k >> s;
	s = ' '+s;

	vector<ll> nearj(n+5) , nearo(n+5),neari(n+5);
	ll lastj = 0 , lasto = 0 , lasti = 0;

	for(ll i = 1; i <= n; i++){

		nearj[i] = lastj;
		nearo[i] = lasto;
		neari[i] = lasti;

		if(s[i] == 'J') lastj = i;
		else if(s[i] == 'O') lasto = i;
		else lasti = i;
	}

	ll ans = INF;

	for(ll i = 1; i <= n; i++){
		if(s[i] == 'I'){

			ll x = i;
			for(ll j = 1; j < k; j++) x = neari[x];

			x = nearo[x];
			for(ll j = 1; j < k; j++) x = nearo[x];

			x = nearj[x];
			for(ll j = 1; j < k; j++) x = nearj[x];

			if(x > 0) ans = min(ans,(i-x+1)-(3*k));
		}
	}

	if(ans == INF) ans = -1;
	cout << ans;
}

int main(){

	fast;

	ll tc = 1;
	// cin >> tc;
	while(tc--) solve();

	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...