C. Vasya and String
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的更多相关文章
- codeforces 676C C. Vasya and String(二分)
题目链接: C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Vasya and String(尺取法)
Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 双端队列 C. Vasya and String
High school student Vasya got a string of length n as a birthday present. This string consists of le ...
- [CF676C]Vasya and String(尺取法,原题)
题目链接:http://codeforces.com/contest/676/problem/C 原题题解链接:http://www.cnblogs.com/vincentX/p/5405468.ht ...
- Codeforces 676C Vasya and String(尺取法)
题目大概说给一个由a和b组成的字符串,最多能改变其中的k个字符,问通过改变能得到的最长连续且相同的字符串是多长. 用尺取法,改变成a和改变成b分别做一次:双指针i和j,j不停++,然后如果遇到需要改变 ...
随机推荐
- Linux指令--rcp,scp
rcp代表"remote file copy"(远程文件拷贝).该命令用于在计算机之间拷贝文件.rcp命令有两种格式.第一种格式用于文件到文件的拷贝:第二种格式用于把文件或目录拷贝 ...
- maven系列--settings.xml
安装maven,会有一份settings.xml文件,我们可以理解这份文件就是用来管理本地maven的一系列东西的.这里我先贴出一份完整的原始xml. <?xml version="1 ...
- 输入和输出--File类
输入和输出 写在前面的:Java中的输入和输出还是比较重要的,之前我都没想整理IO和多线程的,但是在研究后面的好多东西时候,经常要读取资源文件,这个时候就不得不回过头来整理IO了.要玩熟Java的输入 ...
- <<Senium2自动化测试>>读书笔记一
为进一步加强Python知识扩展和学习,在朋友的推荐下选择了<<Selenium2自动化测试实战>>,作者胡志恒,基于Python语言实现,以实例的方式详细讲解WebDrive ...
- struts 中自定义action访问方法
struts中action类继承了ActionSupport 默认实现了execute()方法 struts.xml配置文件中 然后可以配置如下映射: <package name =" ...
- 华硕笔记本电脑Win10改Win7设置U盘启动
华硕笔记本电脑Win10改Win7设置U盘启动 尝试开机按ESC选择前面没有UEFI项的USB启动: 1,在BIOS设置里advanced菜单,把 Lgeacy USB support选择为enabl ...
- Effective Java 之-----返回零长度的数组或集合而不是null
如下代码,通常用户列表为空时,会习惯性返回null,因为这时会认为:null返回值比零长度数组更好,因为它避免了分配数组所需要的开销. private final List<UserBean&g ...
- thinkpadE470 win10安装虚拟机时出现‘intel VT-x’处于禁用状态 无法执行64位操作系统
上图为安装虚拟机时弹出的问题. 解决办法: 本人电脑型号为thinkpadE470.遇到此问题查阅了很多,都是针对一般电脑的.折腾了好一会儿.故特写一篇针对此型号的. 1.重启计算机,一开始出现联想标 ...
- 关于dom4j解析xml
一:相关jar包 dom4j-1.6.1.jar 二:用例xml文件 三:解析 注:可能有的小白不知道如果获取节点,so,you can: for (Iterator<Element> i ...
- setinIerval和setTimeout的区别?
setTimeout和setInterval的使用 这两个方法都可以用来实现在一个固定时间段之后去执行JavaScript.不过两者各有各的应用场景. 方 法 实际上,setTimeout和setIn ...