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 ssh登录的小知识
查看服务器的各个端口: # netstat -tulnp 或者 #netstat -tnip 筛选在后面添加 |grep *** 准许root登录 #vi /etc/ssh/sshd_config 找 ...
- Java并发编程的艺术读书笔记(1)-并发编程的挑战
title: Java并发编程的艺术读书笔记(1)-并发编程的挑战 date: 2017-05-03 23:28:45 tags: ['多线程','并发'] categories: 读书笔记 --- ...
- Hibernate的五个主要接口
Hibernate作为持久成中间件,它的具体实现对与上层调用是透明的,即上层通过接口来调用Hibernate的具体实现,所以对于入门级别的讨论来说,自然应该先从接口开始了.
- 20165318 预备作业二 学习基础和C语言基础调查
20165318 学习基础和C语言基础调查 技能学习经验 我们这一代人,或多或少的都上过各种兴趣班,舞蹈钢琴画画书法,我也是如此.可这些技能中,唯一能拿的出手的就是舞蹈了.按照<优秀的教学方法- ...
- Django 部署到Nginx
在网上搜了很多篇Django+uwsgi+Nginx的部署文章,忙了一下午头昏脑胀,最终完成了部署.部署文章流程讲解都很好,但在细节上或许缺乏一些注意力,导致我多篇文章来回切换在字里行间寻找蛛丝马迹. ...
- CentOS7下 Java、Tomcat、MySQL、Maven热部署
本文介绍了CentOS7 64位下Java.Tomcat.MySQL.Maven热部署等服务器环境的搭建和调试过程. 学生服务器资源获取方法: 云+校园计划 - 腾讯云 阿里云云翼计划 github ...
- js结合方式:
1. 在页面中使用script标签,在标签中书写js代码.<head><script type="text/javascript"> alert(&q ...
- CentOS的软件包的管理之rpm和yum
在linux上,一个软件包通常由二进制程序,库文件,配置文件和帮助文件组成. 其中: 二进制程序一般都放在/bin,/sbin,/usr/bin,/usr/sbin,/usr/local/bin和/u ...
- PHPExcel生成或读取excel文件
以下是详细代码示例: <?php /** * 以下是使用示例,对于以 //// 开头的行是不同的可选方式,请根据实际需要 * 打开对应行的注释. * 如果使用 Excel5 ,输出的内容应该是G ...
- jQuery源码逐行分析学习01(jQuery的框架结构简化)
最近在学习jQuery源码,在此,特别做一个分享,把所涉及的内容都记录下来,其中有不妥之处还望大家指出,我会及时改正.望各位大神不吝赐教!同时,这也是我的第一篇前端技术博客,对博客编写还不是很熟悉,美 ...