C. Vasya and String
题目链接:http://codeforces.com/contest/676/problem/C

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
  1. 4 2
    abba
output
  1. 4
input
  1. 8 1
    aabaabaa
output
  1. 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".

题意:给定一个长度为n的字符串,字符串只要a和b。现在可以修改k个位置的字符[a->b/ b->a],问包含相同字符的连续子串的长度最长是多少?

思路:滑动窗口。定义L,R下标,当没满足k个修改时,R向右滑,当满足K个时记录最优值然后L向右滑。

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstdio>
  4. #include<string>
  5. #include<cstring>
  6. #include<cmath>
  7. #include<bitset>
  8. using namespace std;
  9. #define INF 0x3f3f3f3f
  10. #define PI 3.14159
  11. const int MAXN=+;
  12. char str[MAXN];
  13. int main()
  14. {
  15. #ifdef kirito
  16. freopen("in.txt","r",stdin);
  17. freopen("out.txt","w",stdout);
  18. #endif
  19. int n,k;
  20. while(~scanf("%d%d",&n,&k)){
  21. scanf("%s",str);
  22. int tota=,totb=;
  23. int L=,R=-,MAXL=-;
  24. while(R+<n)
  25. {
  26. if(min(tota,totb)<=k)
  27. {
  28. R++;
  29. if(str[R]=='a'?tota++:totb++);
  30. if(min(tota,totb)<=k)
  31. {
  32. MAXL=max(MAXL,R-L+);
  33. }
  34. }
  35. else
  36. {
  37. if(str[L]=='a'?tota--:totb--);
  38. L++;
  39. }
  40. }
  41. printf("%d\n",MAXL);
  42. }
  43. return ;
  44. }

Codeforces Round #354 (Div. 2)-C的更多相关文章

  1. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  2. Codeforces Round #354 (Div. 2)-D

    D. Theseus and labyrinth 题目链接:http://codeforces.com/contest/676/problem/D Theseus has just arrived t ...

  3. Codeforces Round #354 (Div. 2)-B

    B. Pyramid of Glasses 题目链接:http://codeforces.com/contest/676/problem/B Mary has just graduated from ...

  4. Codeforces Round #354 (Div. 2)-A

    A. Nicholas and Permutation 题目链接:http://codeforces.com/contest/676/problem/A Nicholas has an array a ...

  5. Codeforces Round #354 (Div. 2) D. Theseus and labyrinth

    题目链接: http://codeforces.com/contest/676/problem/D 题意: 如果两个相邻的格子都有对应朝向的门,则可以从一个格子到另一个格子,给你初始坐标xt,yt,终 ...

  6. Codeforces Round #354 (Div. 2) C. Vasya and String

    题目链接: http://codeforces.com/contest/676/problem/C 题解: 把连续的一段压缩成一个数,对新的数组求前缀和,用两个指针从左到右线性扫一遍. 一段值改变一部 ...

  7. Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)

    题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...

  8. Codeforces Round #354 (Div. 2) E. The Last Fight Between Human and AI 数学

    E. The Last Fight Between Human and AI 题目连接: http://codeforces.com/contest/676/problem/E Description ...

  9. Codeforces Round #354 (Div. 2) D. Theseus and labyrinth bfs

    D. Theseus and labyrinth 题目连接: http://www.codeforces.com/contest/676/problem/D Description Theseus h ...

随机推荐

  1. App主界面Tab实现方法

    ViewPager + FragmentPagerAdapter 这里模仿下微信APP界面的实现 国际惯例,先看下效果图:   activity_main.xml 布局文件: <?xml ver ...

  2. 快速入门GreenDao框架并实现增删改查案例

    大家的项目中不可避免的使用到SQLite,为此我们要花费心思编写一个增删改查框架.而一个好的ORM框架则能够给我们带来极大的方便,今天给大家讲解一个非常火热的ORM-GreenDao. 基本概念 Gr ...

  3. 【linux】学习6

    鸟哥13章的东西 shell script速度较慢,适合用于系统管理,但不适合处理大量数值运算 var=$((运算内容)) 可以用来做变量的加减乘除求余运算 total=$(($firstnum*$s ...

  4. 【python】正则中的group()

    来源:http://www.cnblogs.com/kaituorensheng/archive/2012/08/20/2648209.html 正则表达式中,group()用来提出分组截获的字符串, ...

  5. ios NSLayoutConstraint

    为了让我们的应用在不同尺寸的屏幕下都能 “正常”的表示,我们尽量不要把数据写死.大多数可视元素都是一个矩形区域,当然这个矩形区域有坐标的,我们有了这个区域坐标就能确定可视元素的现实位置了.但是ipho ...

  6. springmvc上传List,

    @RequestMapping("pay") public ModelAndView pay(String orderNo, TransactionDTO transaction, ...

  7. Android 更改字体

    1. 将字体ttf文件放在assets目录下 2. 使用: Typeface mTypeFaceLight = Typeface.createFromAsset(context.getAssets() ...

  8. MVC - 11(下)jquery.tmpl.js +ajax分页

    继续 mvc-11(上).dto:http://www.cnblogs.com/tangge/p/3840060.html jquery.tmpl.js 下载:http://pan.baidu.com ...

  9. 使用 Laravel 框架:成为微信公众平台开发者

    转: http://ninghao.net/blog/1441 作者:王皓发布于:2014-05-30 13:16更新于:2014-05-31 12:05 我们可以使用Laravel 框架为微信公众平 ...

  10. 那些年,我们在Django web开发中踩过的坑(一)——神奇的‘/’与ajax+iframe上传

    一.上传图片并在前端展示 为了避免前端整体刷新,我们采用ajax+iframe(兼容所有浏览器)上传,这样用户上传之后就可以立即看到图片: 上传前: 上传后: 前端部分html: <form s ...