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’,另外一次倒过来。在换字母的时候,主要方法就是尺取法了。细节有一点多,需要多考虑一下。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=1e5+100;
string s;
int n,k;inline int fcs(char c)
{int l=0,r=0;int _max=0,sum=0;while(r<n){while(sum<=k&&r<n){if(s[r]==c) sum++;r++;}if(sum>k) r--,sum--;_max=max(_max,r-l);while(sum>=k&&l<n){if(s[l]==c) sum--;l++;}}return _max;
}
int main()
{scanf("%d%d",&n,&k);cin>>s;printf("%d\n",max(fcs('a'),fcs('b')));return 0;
}

努力加油a啊,(o)/~

Vasya and String CodeForces - 676C(尺取法)相关推荐

  1. Codeforces 1167E 尺取法

    题意:给你一个长度为n的数组,以及数组中的数的取值范围1 - m,问有多少个区间[l, r],使得删除了数组中数值为[l, r]的数之后,数组是非递减的. 思路:我们记录一下每一个数出现的最左端和最右 ...

  2. 【HDU - 5672】String(尺取法)

    题干: There is a string SS.SS only contain lower case English character.(10≤length(S)≤1,000,000)(10≤le ...

  3. Longest k-Good Segment CodeForces - 616D(尺取法)

    The array a with n integers is given. Let's call the sequence of one or more consecutive elements in ...

  4. Codeforces Round #364 (Div. 2)C. They Are Everywhere(尺取法)

    题目链接: C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes input ...

  5. Codeforces 1548B Integers Have Friends 尺取法 Hdu 7073 Integers Have Friends 2.0 力能扛鼎随机算法

    文章目录 题意 题解 CF1548B 题解 Hdu 7073 CF1548B HDU7073 题意 定义数的好友组为一个集合SSS,取正整数m>1,∀x∈s,xmodmm>1,\foral ...

  6. B. Complete the Word (尺取法)

    题目链接:http://codeforces.com/problemset/problem/716/B 题目大意:字符串中的 '?' 可以用A->Z任何一个来替换 问能不能通过替换使字符串有一个 ...

  7. QDUOJ 河老师的新年礼物(尺取法)

    河老师的新年礼物 发布时间: 2017年1月1日 15:11   最后更新: 2017年1月1日 15:13   时间限制: 1000ms   内存限制: 256M 描述 河老师的新年礼物是一个长度为 ...

  8. 【常用技巧精选】尺取法

    整理的算法模板合集: ACM模板 目录 1.反向扫描 1 找指定和的整数对 A.UVA1121 Subsequence B.POJ 3320 Jessica's Reading Problem C.l ...

  9. ACM—各种模拟 总结(字符串,尺取法,数学问题)习题汇总

    目录 一.字符串模拟 二. 高精度计算 1. 回文数(高精度,进制转换) 三.数学问题模拟 四.尺取法(双指针法) 1.都说小镇的切糕贵 (尺取法,字符串) umi和弓道 五.奇怪的模拟 x的位数=l ...

最新文章

  1. python入门基础语发_Python基础入门-基础语法
  2. html中dir标签的作用是什么意思,htmldir标签是干啥的?dir标签的具体定义和属性介绍...
  3. JS-数组和函数冒泡排序递归函数
  4. UI设计图标素材|如何应用合适图标
  5. 有个码龄10年的程序员跟我说:“他编程从来不用鼠标”,我说:
  6. 计算机联锁接口,计算机联锁接口设计规范.doc
  7. 超详细讲解ArcGIS拓扑分析(附路网练习数据下载)
  8. mbedTLS中的握手建立操作
  9. The kernel appears to have died. It will restart automatically
  10. adb native raact 夜神_如何用第三方Android模拟器调试react-native?
  11. 软件工程毕业设计课题(57)微信小程序毕业设计JAVA投票评选小程序系统设计与实现
  12. php 短信接口验证码,PHP代码示例_PHP验证码短信接口 | 微米-中国领先的短信彩信接口平台服务商...
  13. Python 计算思维训练——输入和错误处理练习(一)第三关
  14. 服务器最多支持几个远程用户,服务器最大连接数量又超了?win2003系统远程桌面多用户连接设置图文教程...
  15. android视频播放器ui,ArtVideoPlayer 一个灵活的Android视频播放器,支持全屏,小屏播放...
  16. SAP 特征 Classification
  17. 详细的微信小程序讲解
  18. UC书签导入导出搞定
  19. 算法------手写LRU算法
  20. Spark机器人-神经计算棒

热门文章

  1. 微信小程序websocket连接服务器(接收信息)
  2. IOS开发基础之解压缩文件技术
  3. 关于Visual Studio 2017安装需要注意的细节
  4. 说明一项最有成就感的php项目,2020 Github 上 10 个最受欢迎的 PHP 项目
  5. python字符串添加_python字符串的增删改查
  6. html写个用户协议,五分钟学会HTML5的WebSocket协议
  7. php 声波图,Canvas 美丽的声波图
  8. python设计大赛_GitHub - FatBallFish/Multimedia-Python: 多媒体设计大赛-Python后端
  9. python爬取国内代理ip_Python语言爬取代理IP
  10. js动态创建对象_JS深浅拷贝的深入浅出