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;
string s;
int k;
deque<int> d[3];
char c[3] = {'J', 'O', 'I'};
int last[3];
void rem(int x)
{
while((int)d[x].size() > k)
d[x].pop_front();
}
void update(int x, int y)
{
rem(x);
for(int l = last[y] + 1; l < d[x].front(); l++)
if(s[l] == c[y]) d[y].push_back(l);
last[y] = d[x].front() - 1;
}
void add_letter(int x)
{
int l;
for(l = 0; l < 2; l++)
{
if((int)d[l].size() < k)
{
if(s[x] == c[l])
{
d[l].push_back(x);
last[l] = x;
}
if(d[l].size() == 1)
{
while(l)
{
update(l,l-1);
l--;
}
rem(0);
}
return;
}
}
if(s[x] != c[2]) return;
d[2].push_back(x);
last[2] = x;
update(2,1);
update(1,0);
rem(0);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, l, ans = -1;
cin>>n>>k>>s;
for(l = 0; l < n; l++)
{
add_letter(l);
if((int)d[2].size() == k)
{
int cans = d[2].back() - d[0].front() + 1 - k * 3;
if(ans == -1 || ans > cans) ans = cans;
}
}
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... |