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不停++,然后如果遇到需要改变 ...
随机推荐
- 六、Html头部和元信息
前面整理的都是html常用到的标签,这里整理一下html的的头部和元信息标签. 定义html都的头部要写在<head>标签里面,一般他还包含如下一些标签: 1,<script> ...
- Java 生产者消费者模式详细分析
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
- SQL语句-delete语句
delete语句 delete语句用于删除表中已经存在的整行数据 Tbl_name关键词代表删除数据的目标表 Where子句代表被删除数据的满足条件,如果没有where子句则代 表所有表数据都删除 O ...
- ubuntu网桥设置
什么是桥接? 桥接(Bridging),是指依据OSI网络模型的链路层的地址,对网络数据包进行转发的过程,工作在OSI的第二层.一般的交换机,网桥就有桥接作用. 一般的交换机,网桥就有桥接作用.就交换 ...
- [PHP]PDO占位符预处理在 IN 和 LIKE 中用法
两点注意项: 1. 占位符 (?) 必须被用在整个值的位置,不需要引号等其它字符. 2. 参数按数组元素顺序依次传递给占位符. <?php /** * PDO基于占位符的查询预处理 * * @l ...
- spring boot 中实现兼容不同的请求类型的方法。
比如一个接口,既想实现请求参数是application/json,又想实现form提交,改怎么做呢?用postman去测试,发现不可能做到两全其美. 我有一个方法,就是不用requestbody,也可 ...
- Django跨域请求之JSONP和CORS
现在来新建一个Django项目server01,url配置为 url(r'^getData.html$',views.get_data) 其对应的视图函数为get_data: from django. ...
- 免费分享Navicat12各版本破解(仅供学习使用)
花了几个小时终于找到比较"绿色"的安装包了...(无力吐槽下现在网上好多挂羊头卖狗肉的网站) https://pan.baidu.com/s/1geMIkfT 密码:9a98 里面 ...
- .NET Core 2.0 问题杂记
一. Ubuntu 14.04 安装 1.安装之前请先删除之前的.net core 版本 命令如下: 1.1 获取安装的.net core 版本 sudo apt --installed list | ...
- SDOI2017 Round1
SDOI2017 Round1 在回去的车上写的 cnblog的markdown貌似有bug,空行都没有了 Day -several [清明节] 没想到在省选之前还会有一次放假 放假前一天晚上走到校门 ...