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不停++,然后如果遇到需要改变 ...
随机推荐
- kindeditor使用
下载地址http://kindeditor.net/down.php @官方文档 使用步骤: 引入js <script charset="utf-8" src="r ...
- Windows脚本相关
1 获取IP地址 echo StartChangeIPFile echo 获取主机名 for /f %%i in ('hostname') do (set pcName=%%i) ::ping %pc ...
- ProgressDialog的使用及逻辑处理
一般用的情况先声明一个ProgressDialog progressShow = true;(用来判断用户是否点击了取消键) final ProgressDialog pd = new Progres ...
- 达尔稳usb转RJ45的接口转换器(usb2.0接口)在ubuntu16.04中驱动(r8152)编译安装与使用
淘宝买了usb转RJ45的接口转换器:https://detail.tmall.com/item.htm?id=524808012954&ali_refid=a3_430582_1006:11 ...
- Zabbix3.4部署
Zabbix简介 zabbix(音同 zæbix)是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活 ...
- 使用H2数据库进行单元测试
背景 H2 数据库是一个开源的嵌入型内存数据库,采用纯Java语言实现: 程序非常小巧轻便,整个完整的Jar包也只有1.5M左右,很容易集成到项目中. 官网地址 http://www.h2databa ...
- JAVA并发编程学习笔记------锁顺序死锁
一.需求描述: 将资金从一个账户转移到另一个账户. 二.程序实现: (1)账户类: public class Account { private long account; public Accoun ...
- 浅谈大型web系统架构(一)
目录 Web前端系统 负载均衡系统 数据库集群系统 缓存系统 分布式存储系统 分布式服务器管理系统 代码发布系统 动态应用,是相对于网站静态内容而言,是指以c/c++.php.Java.perl. ...
- python各种运算优先级一览表
##python各种运算的优先级 运算符 描述 lambda Lambda表达式 or 布尔"或" and 布尔"与" not x 布尔"非" ...
- Python基础篇(六)
retun空值,后面的语句将不再被执行 >>> def test(): ... print("just a test!") ... return .. ...