原题链接

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

  1. #include<iostream>
  2. using namespace std;
  3. char str[100005];
  4. int main(void)
  5. {
  6. int n,k;
  7. cin>>n>>k;
  8. for(int i=0;i<n;i++)
  9. {
  10. cin>>str[i];
  11. }
  12. int l=0,r=0,ans=0,numa=0,numb=0;
  13. while(r<n)
  14. {
  15. if(str[r]=='a')
  16. numa++;
  17. else numb++;
  18. if(numa<=k||numb<=k)
  19. {
  20. ans++;r++;
  21. }
  22. else
  23. {
  24. if(str[l]=='a')
  25. numa--;
  26. else numb--;
  27. l++;r++;
  28. }
  29. }
  30. cout<<ans<<endl;
  31. return 0;
  32. }

如有疑问请私信!!

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. maven核心概念--插件和目标

    maven的核心概念有很多,我们这里先来看Plugins和Goals. 在上一篇博客中,我们用两种类型的命令行参数运行了Maven. 第一条命令,create.这是一条单个的插件目标,Archetyp ...

  2. Jmeter_接口自动化基础流程概述

    这里补一篇基础的操作吧,不然总觉得没底! 1:创建一个线程组   2:添加一个cookie管理器   33333 3:设置你的信息头管理器:application/json;text/plain;ch ...

  3. Node.js调用C#代码

    在Node.js的项目中假如我们想去调用已经用C#写的dll库该怎么办呢?在这种情况下Edge.js是一个不错的选择,Edge.js是一款在GitHub上开源的技术,它允许Node.js和.NET c ...

  4. Nginx+Geoserver部署所遇问题总结

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 该问题的最终解决离不开公司大拿whs先生的指点,先表示感谢. ...

  5. APP测试(转载)

    (1) 非功能测试 app测试的一个重要方面是app的非功能需求.移动app在推出市场或进行进一步开发前,测试人员有一定的职责做该类需求的跟踪工作. 早期开发阶段要进行的第一个测试应该是实用性测试.通 ...

  6. sigmoid_cross_entropy_with_logits

    sigmoid_cross_entropy_with_logits 原创文章,请勿转载!!! 函数定义 def sigmoid_cross_entropy_with_logits(_sentinel= ...

  7. Win7系统下彻底删除无用服务的方法

    win7系统下中有非常多的服务项,用户来满足不同行业用户间的所有需求,系统服务也是执行指定系统功能的程序,许多情况下我们想要运行软件或执行外接设备都无法离开系统服务,但并非所有系统服务都是我们用到的, ...

  8. Jsonp 原理简述

    浏览器因为安全问题而有一个同源策略,不允许跨域请求数据.但是 js 脚本资源和图片资源还是被允许跨域请求的.在 web 开发中,遇到要跨域请求 json 数据时,可以用 jsonp 的方式来绕过浏览器 ...

  9. 大白话说Java反射:入门、使用、原理

    文章首发于[博客园-陈树义],点击跳转到原文<大白话说Java反射:入门.进阶.原理> 反射之中包含了一个「反」字,所以想要解释反射就必须先从「正」开始解释. 一般情况下,我们使用某个类时 ...

  10. vue项目基本流程

    一.做项目基本流程: 1.规划组件结构 Nav.vue Header.vue Home.vue..... 2.编写对应路由 vue-router 3.具体些每个组件功能 一些公共的文件jquery,j ...