Vasya and String 取尺法

 Vasya and String
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u


Description

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.

Sample Input

Input
4 2
abba
Output
4
Input
8 1
aabaabaa
Output
5

复制一段dalao的对取尺法的解释感觉很有道理

题意:给你一串字符串和k个修改字符串的机会,让这个字符串获得最长连续相同子串

题解:所谓尺取法,顾名思义,就是一把尺子(固定某一条件),不断向右(向左)移动,不断更细我们要的答案。在这里,我们只要从左往右,让修改的字符个数从0慢慢增加到k,中途将字符改成同一个字符(a改成b或者b改成a都行),最后修改字符数固定为k,每次向右移动时,如果字符串需要修改,那就改掉右面的字符,将之前最左边的字符换回来。那么我们可以用一个队列去实现。


如果你看不懂上面也没关系,我再讲具体一些。我们从左边开始,扫描字符串。如果遇到a,那就把a丢进队列,如果遇到b,且此时队列中b的个数不超过k的话,就把b丢进去。如果b的个数等于k,且又遇到一个b,那就记录下此时队列长度,这个时候队列里k个b可以当作a,所以可以记录队列元素个数,更新答案最大值。之后将队列前面元素弹出,直到弹出一个b,再将新的b压进队列……一直扫描直到字符串尾。


接下来将a和b互换,重复上面步骤,更新最大值。最后输出最大值。


#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>

using namespace std;

char a[1000005];

int main()
{
    //freopen("input.txt","r",stdin);
    int n,k;
    while(~scanf("%d%d",&n,&k))
    {
        scanf("%s",&a);
        queue<char> q;

        int maxn=0;
        int len=0;

        for(int i=0;i<n;i++)
        {
            if(a[i]=='a')
                q.push(a[i]);
            else if(len<k)
            {
                q.push(a[i]);
                len++;
            }
            else
            {
                
                maxn=max(maxn,(int)q.size());
                while(!q.empty()&&q.front()=='a')
                    {
                        q.pop();
                    }
                if(!q.empty())
                {
                    q.pop();
                    q.push(a[i]);
                }
            }
        }
        maxn=max(maxn,(int)q.size());
        while(!q.empty())
        {
            q.pop();
        }
        len=0;
         for(int i=0;i<n;i++)
        {
            if(a[i]=='b')
                q.push(a[i]);
            else if(len<k)
            {
                q.push(a[i]);
                len++;
            }
            else
            {
                
                maxn=max(maxn,(int)q.size());
                while(!q.empty()&&q.front()=='b')
                    {
                        q.pop();
                    }
                if(!q.empty())
                {
                    q.pop();
                    q.push(a[i]);
                }
            }
        }
        maxn=max(maxn,(int)q.size());
        printf("%d\n",maxn);

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值