原题链接

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 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

Input

4 2

abba

Output

4

Input

8 1

aabaabaa

Output

5

Note

In the first sample, Vasya can obtain both strings “aaaa” and “bbbb”.

In the second sample, the optimal answer is obtained with the string “aaaaabaa” or with the string “aabaaaaa”.

大致题意

给你一串只有a和b构成的字符串,字符串长度为n,可修改字符限制个数为k,问能得到的最长连续相同的字符串的长度。非常明显的尺取法的应用。

思路

从左到右扫描,对a和b出现个数计数,只要满足a的个数或者b的个数小于等于k,就把长度加一,否则就将尺子右移(即尺子的开头指针+1,末尾指针+1),如果尺子开头指针的字符为a,则a的计数-1,若果字符是b,则b的计数-1,一直扫描到字符串末尾。

AC代码

#include<iostream>
using namespace std;
char str[100005];
int main(void)
{
    int n,k;
    cin>>n>>k;
    for(int i=0;i<n;i++)
    {
    cin>>str[i];
    }
    int l=0,r=0,ans=0,numa=0,numb=0;
    while(r<n)
    {
        if(str[r]=='a')
        numa++;
        else numb++;
        if(numa<=k||numb<=k)
        {
        ans++;r++;
        }
        else
        {
            if(str[l]=='a')
            numa--;
            else numb--;
            l++;r++;
        }
    }
    cout<<ans<<endl;
    return 0;
}

如有疑问请私信!!

C. Vasya and String的更多相关文章

  1. codeforces 676C C. Vasya and String(二分)

    题目链接: C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input sta ...

  2. Vasya and String(尺取法)

    Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  3. Codeforces Round #354 (Div. 2) C. Vasya and String 二分

    C. Vasya and String 题目连接: http://www.codeforces.com/contest/676/problem/C Description High school st ...

  4. codeforces 354 div2 C Vasya and String 前缀和

    C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. Codeforces Round #354 (Div. 2)——C. Vasya and String(尺取)

    C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Codeforces Round #354 (Div. 2)-C. Vasya and String,区间dp问题,好几次cf都有这种题,看来的好好学学;

    C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. 双端队列 C. Vasya and String

    High school student Vasya got a string of length n as a birthday present. This string consists of le ...

  8. [CF676C]Vasya and String(尺取法,原题)

    题目链接:http://codeforces.com/contest/676/problem/C 原题题解链接:http://www.cnblogs.com/vincentX/p/5405468.ht ...

  9. Codeforces 676C Vasya and String(尺取法)

    题目大概说给一个由a和b组成的字符串,最多能改变其中的k个字符,问通过改变能得到的最长连续且相同的字符串是多长. 用尺取法,改变成a和改变成b分别做一次:双指针i和j,j不停++,然后如果遇到需要改变 ...

随机推荐

  1. 记录一次tomcat下项目没有加载成功

    哥们儿实在太low了,web.xml文件中加载的spring mybatis配置文件和配置的文件不是同一个文件名导致的!

  2. alwaysOn中关于维护计划的应用方案

    由于alwaysOn环境下主副本所在的实际服务器不固定, 所以我目前采取的方案是创建维护计划的时候, 在各个服务器上创建一份维护计划. (假设有2个服务器需要故障转移, 那么就在这两个服务器上分别创建 ...

  3. python源码书籍

    <Python源码剖析>一书现在很难买到,目前大部分都是电子书. 为了更好地利用Python语言,无论是使用Python语言本身,还是将Python与C/C++交互使用,深刻理解Pytho ...

  4. IDEA精髓快捷键

    删除一行:Ctrl+X 快速查找:Ctrl+F 打开文件目录结构: Ctrl+F12 可以把代码包在一个块内:Ctrl+Alt+T 替换文本:Ctrl+R, Alt+Shift+Up/Down,上/下 ...

  5. Asp.net mvc 中View 的呈现(二)

    [toc] 上一节介绍了 Asp.net mvc 中除 ViewResult 外的所有的 ActionResult,这一节介绍 ViewResult. ViewResultBase ViewResul ...

  6. 渗透测试,form对象类型转换,简单demo

    最近公司的项目在进行国家某行业的安全检测,涉及到项目安全渗透等方面的问题: 参与项目的渗透等改造,是一个机遇与挑战,今后对与项目安全等方面会思考更多: 下面说说form表单对象提交,为了防止抓包,后台 ...

  7. 程序管理与SElinux

    一.程序: 1.在Linux中,触发任何一个事件是,系统都会将他定义为一个程序,并且给予这个程序一PID,同时依据启发这个程序的使用者与相关属性关系,给予这个PID一组有效的权限设定,从此以后,这个P ...

  8. 给php加速安装APC

    说明:APC-3.1.13 适应于 php-5.4.27 下载: wget http://blog.xinfilm.com/softdir/APC-3.1.13.tgz tar -zxvf APC-3 ...

  9. BZOJ 1488: [HNOI2009]图的同构 [Polya]

    完全图中选出不同构的简单图有多少个 上题简化版,只有两种颜色....直接copy就行了 太诡异了,刚才电脑上多了一个不动的鼠标指针,然后打开显卡管理界面就没了 #include<iostream ...

  10. 【转】TCP/IP和SOCKET的区别

    要写网络程序就必须用Socket,这是程序员都知道的.而且,面试的时候,我们也会问对方会不会Socket编程?一般来说,很多人都会说,Socket编程基本就是listen,accept以及send,w ...