Vasya and String (feet emulated)

High school student Vasya got a string of length n as a birthday present. This string consists of letters ‘a’ and ‘b’ only. Vasya denotes beauty 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.

Examples
the Input
. 4 2
Abba
the Output
. 4
the Input
. 8. 1
aabaabaa
the Output
. 5
Title Analysis:
foot emulated: As the name suggests, as a ruler as taking some borrow challenge above book words, foot emulated usually array keep a sum index, i.e., the selected about the interval endpoints, and then continue to push forward the end zone to get the answer about the actual situation. The need to master this skill, because the foot is much higher than the direct borrowing from violence enumeration range efficiency, especially when a large amount of data, so the scale of borrowing is a highly effective method for the enumeration range, generally used for obtaining certain interval limits the number of shortest interval or the like. Of course, there are any tricks its deficiencies, in some cases borrowing foot is not feasible, we can not come to the correct answer.
This passage is taken from the following article.

Original link: https: //blog.csdn.net/consciousman/article/details/52348439

For this problem, that is, the scan from the beginning, according to the principle of greed, the only change that only a or b characters maximum possible length of the sequence, so we cycle twice. They are removed and a character, character b
specific implementation process is:

int t1=k,t2=k;//把k存储一下
    int left=0,right=0;//从左向右移动的指针
while(left<right&&right<n)//先去掉a字符
    {
        if(s[right]=='a') t1--;
        if(t1<0)//当遇到k+1次a时
        {
            while(s[left]=='b') left++;//当遇到a时停止
            left++;//使当前left到right序列内没有多余a
            t1=0;
        }
        maxl=max(maxl,right-left+1);//取最优值
        right++;
    }

Right pointer from left to right, as the number of times the upper limit of k can be modified, to take to remove a character, for example, once per encounter a character, it is k-, when k == 0, a description of all times have run out, then if we encounter a character, then it becomes a k-1, this time is less than zero, then in order to maintain the maximum number of times you use, it is necessary to find a first character you meet, it will be removed, k is 0 then the assignment, equivalent to update a sequence, well, we will be able to follow this line of thought, practicality is still quite high. The first time will use less, take the paper to simulate a small data again, this time my mind there is a framework, and then encountered a similar problem is also very easy to think.
Complete code:

#include<iostream>
using namespace std;
const int max_n=1e5+10;
char s[max_n];
int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=0;i<n;i++)
        cin>>s[i];
    int t1=k,t2=k;//把k存储一下
    int left=0,right=0;//从左向右移动的指针
    int maxl=-1;
    while(left<right&&right<n)//先去掉a字符
    {
        if(s[right]=='a') t1--;
        if(t1<0)//当遇到k+1次a时
        {
            while(s[left]=='b') left++;//当遇到a时停止
            left++;//使当前left到right序列内没有多余a
            t1=0;
        }
        maxl=max(maxl,right-left+1);//取最优值
        right++;
    }
    left=0;right=0;
    while(left<=right&&right<n)//这次该去除b啦
    {
        if(s[right]=='b') t2--;
        if(t2<0)
        {
            while(s[left]=='a') left++;
            left++;
            t2=0;
        }
        maxl=max(maxl,right-left+1);
        right++;
    }
    cout<<maxl;
    return 0;
}
Published 42 original articles · won praise 42 · views 9311

Guess you like

Origin blog.csdn.net/amazingee/article/details/104534150