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 ;
}

codeforces 676C的更多相关文章

  1. Codeforces 676C Vasya and String(尺取法)

    题目大概说给一个由a和b组成的字符串,最多能改变其中的k个字符,问通过改变能得到的最长连续且相同的字符串是多长. 用尺取法,改变成a和改变成b分别做一次:双指针i和j,j不停++,然后如果遇到需要改变 ...

  2. codeforces 676C C. Vasya and String(二分)

    题目链接: C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input sta ...

  3. Codeforces 划水

    Codeforces 566F 题目大意:给定$N$个数,任意两个数之间若存在一个数为另一个数的因数,那么这两个数存在边,求图中最大团. 分析:求一个图最大团为NP-Hard问题,一般不采用硬方法算. ...

  4. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  7. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  8. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  9. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

随机推荐

  1. eclipse F6和F8的问题

    # 首先大致说明一下F6和F8的作用: | 在debug模式下, F6的作用是跳到下一步,F8的作用是跳到下一个断点 # 情景: | 在eclipse以debug模式同时启动两个项目,并且两个项目都打 ...

  2. js动态实现时分秒

    <div id="time" style="color: #96C2DD;</div>      <script type="text/ ...

  3. ubuntu系统在安装好mysql后,出现ERROR 2002(HY000: Can't to local MySQL server through socket '/var/run/mysqld/mysqld.sock')(2)(图文详解)

    不多说,直接上干货! 问题详情 我在写此博客之前,看了网上各种资料,写的太冗余和繁琐杂乱.最简单的解决方法莫过于我这篇博客.直接如下. 这是liux套接字网络的特性,win平台不会有这个问题. 解决方 ...

  4. kpw2使用心得

    一:截屏 只需要同时按下对角线的两个点(比如左下角,右上角)就可以,图片可以通过数据线连接到电脑,在根目录下就可以查看截屏图片. 二:拖曳电子书pdf 可以通过数据线连接,找到kpw2的磁盘,将pdf ...

  5. 【Linux】Linux下使用Docker快速部署Oracle数据库

    安装最新Docker wget -qO- https://get.docker.com/ | sh` 安装 docker-compose sudo curl -L https://github.com ...

  6. Linux 下安装 Memcached 和 PHP 开启 Memcached 扩展 及 LAMP 环境的安装

    http://blog.csdn.net/liruxing1715/article/details/8269563

  7. 原生js与jquery的区别

    1.选择器: js: $('.car_img_con img')[0]; var jsObj = document.getElementsByClassName('sel_index_block')[ ...

  8. java基础之登录程序

    注:此版本仅供学习使用! Login.java import java.awt.Font; import java.awt.event.*; import java.sql.*; import jav ...

  9. 什么是PV,什么是UV,什么是IP. 流量统计的各种数据!

    pv流量 什么是PV? 解答:PV是指页面刷新的次数,每一次页面刷新,就算做一次pv流量. PV高一定代表来访者多吗? 解答:不一定如此,一般来说,PV与来访者的数量成正比,但是PV并不直接决定页面的 ...

  10. 网站部署中遇到的问题-网页中js,css和图片资源无法加载

    问题描述: 打开的网页跑版,图片无法加载,用控制台调试发现css和js都没有加载. 原因: 没有启用IIS"静态内容". 解决方法: 设置"打开或关闭windows功能& ...