C. Vasya and String
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Copy
4 2
abba
output
4
input

Copy
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的字符串,你能够挑出<=k个字母来,让他变化成你想要的字母,问,在此变换下能够得到的最长的连续相同子串的长度是多少?

分析:

分两种情况,要连续a和连续b。

连续a 时,记录前面有多少个b字母,在这个上面进行尺取就行了。

#include <bits/stdc++.h>

using namespace std;

const int maxn = ;
char str[maxn];
int numb[maxn];
int numa[maxn]; int main()
{
//freopen("in.txt","r",stdin);
int n,k;
scanf("%d%d",&n,&k);
scanf("%s",str+); for(int i = ; i <= n; i++) {
if(str[i]=='b')
numb[i] = numb[i-] + ;
else numb[i] = numb[i-]; if(str[i]=='a')
numa[i] = numa[i-] + ;
else numa[i] = numa[i-];
} int L = ;
int R = ;
int ans = ;
int tmp = ;
int pos = ;
while(R<=n) {
while(R<=n&&numb[R]-tmp<=k) {
ans = max(ans,R-pos);
R++;
}
pos++;
tmp=numb[L];
L++;
} L = ;R = ;
tmp = ;
pos = ;
while(R<=n) {
while(R<=n&&numa[R]-tmp<=k) {
ans = max(ans,R-pos);
R++;
}
pos++;
tmp=numa[L];
L++;
} printf("%d\n",ans); return ;
}

最新文章

  1. iOS - 滑屏方案
  2. 通过寄生组合式继承创建js的异常类
  3. swiftlint升级
  4. unique踢出相同元素
  5. SQL Server 监控 使用sp_trace_create
  6. python工厂方式创建list
  7. 使用NIO提升性能
  8. bnuoj 20834 Excessive Space Remover(水水)
  9. Ambiguous mapping found. Cannot map &#39;xxxxController&#39; bean method
  10. idea编译工程时出现Error:java: 无效的目标发行版: 1.8
  11. zyUpload界面绝佳、体验超棒的HTML5上传插件
  12. MongoDB应用介绍之前
  13. console深入理解
  14. python 简单验证码 random模块
  15. 单例模式与静态变量在PHP中
  16. ASP.NET Core DevOps
  17. Error: Default interface methods are only supported starting with Android N (--min-api 24): java.io.InputStream org.apache.poi.sl.usermodel.ObjectShape.readObjectData()
  18. python sub替换方法
  19. 实验吧—Web——WP之 上传绕过
  20. Js页面刷新前提示-jquery页面刷新事件

热门文章

  1. opatch on-line patch and standby-fisrt patch
  2. (转)在 Windows 上调优 DB2 数据库的八个简单步骤
  3. (转)Cobbler无人值守批量安装Linux系统
  4. 牛客网Java刷题知识点之为什么HashMap不支持线程的同步,不是线程安全的?如何实现HashMap的同步?
  5. kafka配置文件中参数的限制
  6. 8086中断系统——《x86汇编语言:从实模式到保护模式》读书笔记04
  7. vim 配置文件——部分配置
  8. java使用netty的模型总结
  9. 51nod 1245 Binomial Coefficients Revenge
  10. Hashtable元素的遍历