双端队列 C. Vasya and String

Posted Aragaki

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了双端队列 C. Vasya and String相关的知识,希望对你有一定的参考价值。

High school student Vasya got a string of length n as a birthday present. This string consists of letters ‘a‘ and ‘b‘ only. Vasya denotesbeauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.

Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?

Input

The first line of the input contains two integers n and k (1?≤?n?≤?100?000,?0?≤?k?≤?n) — the length of the string and the maximum number of characters to change.

The second line contains the string, consisting of letters ‘a‘ and ‘b‘ only.

Output

Print the only integer — the maximum beauty of the string Vasya can achieve by changing no more than k characters.

 

 

string s;
int main()
{
int n,k;
while(cin>>n>>k){
cin>>s;

int start = 0;
int ans = 0;
deque<int> De;
for(int i = 0;i < s.size();i ++){
if(s[i] == ‘b‘) De.push_back(i);
if(De.size() > k){
start = De.front()+1;
De.pop_front();
}
ans = max(ans,i-start+1);
}
De.clear();
start = 0;
for(int i = 0;i < s.size();i ++){
if(s[i] == ‘a‘) De.push_back(i);
if(De.size() > k){
start = De.front()+1;
De.pop_front();
}
ans = max(ans,i-start+1);
}
cout<<ans<<endl;
}
return 0;
}

以上是关于双端队列 C. Vasya and String的主要内容,如果未能解决你的问题,请参考以下文章

C. Vasya and Multisets

C. Vasya And The Mushrooms

cf#512 C. Vasya and Golden Ticket

Codeforces1187 C. Vasya And Array(思维,构造)

Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 二分 + 尺取

Codeforces Round #512 (Div. 2, based on Technocup 2019 Elimination Round 1) C. Vasya and Golden Tick