#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define pa pair<ll, ll>
#define fi first
#define se second
#define bit(mask, j) ((mask >> j) & 1)
#define t_test int t;cin >> t;while(t--)
const ll mod = 1e9 + 7;
const ll INF = 1e18;
inline void adm(ll &x){if(x>=mod)x%=mod;else if(x<0)x+=mod;}
//--------------------------------------------------------------------
string t = "JOI";
const ll N = 2e5 + 10;
ll nxt[N][3], cnt[3], L[3], c[N];
void hbmt() {
ll n, k;
cin >> n >> k;
string s;
cin >> s;
s = ' ' + s;
FOR(j, 0, 2) nxt[n + 1][j] = n + 1;
FORD(i, n, 0) {
FOR(j, 0, 2) nxt[i][j] = nxt[i + 1][j];
if(i != 0)
nxt[i][t.find(s[i])] = i;
}
FOR(i, 1, n) {
ll kt = t.find(s[i]);
cnt[kt]++;
if(cnt[kt] >= k) {
L[kt] = nxt[L[kt] + 1][kt];
c[L[kt]] = i;
}
}
ll ans = INF;
FOR(i, 1, n) {
if(c[i]) {
if(c[nxt[c[i]][1]]) {
if(c[nxt[c[nxt[c[i]][1]]][2]]) {
ans = min(ans, c[nxt[c[nxt[c[i]][1]]][2]] - i + 1 - 3 * k);
}
}
}
}
if(ans == INF) cout << -1;
else cout << ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
if(fopen("hbmt.inp", "r")) {
freopen("hbmt.inp", "r", stdin);
freopen("hbmt.out", "w", stdout);
}
// t_test
hbmt();
return 0;
}
Compilation message (stderr)
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:59:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | freopen("hbmt.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
ho_t2.cpp:60:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | freopen("hbmt.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |