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
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".

题意:给你一串字符串和k个修改字符串的机会,让这个字符串获得最长连续相同子串

题解:所谓尺取法,顾名思义,就是一把尺子(固定某一条件),不断向右(向左)移动,不断更细我们要的答案。在这里,我们只要从左往右,让修改的字符个数从0慢慢增加到k,中途将字符改成同一个字符(a改成b或者b改成a都行),最后修改字符数固定为k,每次向右移动时,如果字符串需要修改,那就改掉右面的字符,将之前最左边的字符换回来。那么我们可以用一个队列去实现。

如果你看不懂上面也没关系,我再讲具体一些。我们从左边开始,扫描字符串。如果遇到a,那就把a丢进队列,如果遇到b,且此时队列中b的个数不超过k的话,就把b丢进去。如果b的个数等于k,且又遇到一个b,那就记录下此时队列长度,这个时候队列里k个b可以当作a,所以可以记录队列元素个数,更新答案最大值。之后将队列前面元素弹出,直到弹出一个b,再将新的b压进队列……一直扫描直到字符串尾。

接下来将a和b互换,重复上面步骤,更新最大值。最后输出最大值。

代码如下

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
char s[];
int main()
{
int n,k;
scanf("%d%d",&n,&k);
scanf("%s",s);
queue <char> Q;
int maxn=;
int len=;
for (int i=; i<n; i++)
{
if (s[i]=='a')
Q.push(s[i]);
else if (len<k)
{
len++;
Q.push(s[i]);
}
else
{
maxn=max(maxn,(int)Q.size());
while (!Q.empty()&&Q.front()=='a')
Q.pop();
if (!Q.empty())
{
Q.pop();
Q.push(s[i]);
}
}
}
maxn=max(maxn,(int)Q.size());
while (!Q.empty()) Q.pop();
len=;
for (int i=; i<n; i++)
{
if (s[i]=='b')
Q.push(s[i]);
else if (len<k)
{
len++;
Q.push(s[i]);
}
else
{
maxn=max(maxn,(int)Q.size());
while (!Q.empty()&&Q.front()=='b')
Q.pop();
if (!Q.empty())
{
Q.pop();
Q.push(s[i]);
}
}
}
maxn=max(maxn,(int)Q.size());
printf("%d\n",maxn);
return ;
}

Vasya and String(尺取法)的更多相关文章

  1. Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)

    题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...

  2. hdu 5672 String 尺取法

    String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem D ...

  3. HDU 5672 String 尺取法追赶法

    String Problem Description There is a string S.S only contain lower case English character.(10≤lengt ...

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

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

  5. 【CF676C】Vasya and String(二分查找,线性扫描尺取法)

    题意: 给出一个长度为n的字符串,只有字符'a'和'b'.最多能改变k个字符,即把'a'变成'b'或把'b'变成'a'. 问改变后的最长连续相同字符的字串长度为多少. 首先是二分查找,好想也好写 .. ...

  6. 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 ...

  7. HDU 5672 String【尺取法】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5672 题意: 有一个10≤长度≤1,000,000的字符串,仅由小写字母构成.求有多少个子串,包含有 ...

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

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

  9. C. Vasya and String

    原题链接 C. Vasya and String High school student Vasya got a string of length n as a birthday present. T ...

随机推荐

  1. 使用SyncToy 同步两台机器上的文件夹

    @echo off echo 准备启动同步... net use \\WIN-AJH8QENQQGK "123456" /user:Administrator Z:\SyncToy ...

  2. oracle 删除当前用户下所有的表、索引、序列...

    select 'drop table "' || table_name ||'";'||chr(13)||chr(10) from user_tables;  --delete t ...

  3. 二十三、oracle pl/sql分类三 包

    包用于在逻辑上组合过程和函数,它由包规范和包体两部分组成.1).我们可以使用create package命令来创建包,如:i.创建一个包sp_packageii.声明该包有一个过程update_sal ...

  4. sql 针对拼接语句的优化

    在日常的开发中尽量少采用拼接语句,但针对多条件联合查询,并有多字段可以偏序的情况下,的确采用拼接语句要方便简单得多,单数据库会因为传入的参数不同而产生不同的计划数,计划数多了,对数据库影响很大. 为了 ...

  5. 分布式版本控制系统Git-----2.上传至远程仓库之基础版

    好,之前已经将文档下载下来了,但是我感觉还是将自己之前截的图放出来比较好,自己整理的,但是总不能放桌面上,时间久了也会忘得,索性放到博客上吧,也便于其他人查看,简直是百利而无一害啊.哈哈.来吧. 注意 ...

  6. js格式转换

    //1.保留整数 function showInteger(value,row,index){ if(value!=null && value!="" && ...

  7. Xcode-之Code Snippets Library

    一.说明 Code Snippets Library 为代码片段库,在开发项目过程中经常会遇到一些重复的代码块,创建代码片段库可以减少我们开发的工作量,而且非常方便调用.Xcode系统中也为我们提供了 ...

  8. Okhttp设置http缓存,在没有网络的情况下加载http缓存里面的内容

    HTTP_CACHE_FILENAME为缓存地址根路径: private final String HTTP_CACHE_FILENAME = "HttpCache"; priva ...

  9. Javaweb 第6天JDBC课程

    JDBC课程 两日大纲 ● JDBC基本操作 ● 预设语句对象 ● 自定义数据库工具类 ● JDBC批处理 ● 事务 ● 连接池 *********************************** ...

  10. mongodb集群+分片部署(二)

    机器:10.165.38.68    10.165.38.72 部署包:mongodb-linux-x86_64-rhel55-3.0.2.tgz(百度云盘下载地址:http://pan.baidu. ...