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. git 基本的操作

      查看分支:git branch   查看所有分支:git branch -a   删除分支:git branch -d <name>   创建分支:git branch <nam ...

  2. struts2查询的数据的存放

    当我们查询数据的时候,把它存放到一个位置.以供页面显示. 1:使用***Map取代内置对象存放 public String query(){ ActionContext.getContext().pu ...

  3. CSS3 Filter滤镜效果

    关注到它是在一次分享会当中,很神奇,只需写一行代码就可以变身很美的视觉效果,这就是CSS3滤镜. 语法 filter:fuction(param); 如今浏览器支持情况相比以前乐观很多,点击查看兼容 ...

  4. 【实验室笔记】C#的Socket客户端接收和发送数据

    采用socket发送和接收数据的实验中,服务器采用的是网络助手作为模拟服务器端. 客户端程序流程: 应用的命名空间: using System.Net; using System.Net.Socket ...

  5. Starting httpd: (98)Address already in use: make_sock: could not bind to address [::]:80

    netstat -tulpn| grep :80 killall -9 httpd /etc/init.d/httpd start  or service httpd start

  6. 重新注册.netframework4.0

    IIS和.netfw4.0安装顺序是从前到后,如果不小心颠倒了,无所谓. 打开程序-运行-cmd:输入一下命令重新注册IIS C:\WINDOWS\Microsoft.NET\Framework\v4 ...

  7. Code Sign error: No code signing identities found: No valid signing identities

    Code Sign error: No code signing identities found: No valid signing identities 解决办法:如果证书可获取,最简办法就是把所 ...

  8. drupal中使用jquery&ajax

    不说了,直接上代码,看注释吧: <!--drupal中使用jquery的方法,有三个不同之处 --> <script> (function($){//1.$符号不放在最外边了 ...

  9. SQL MySQL

    SQL 结构化查询语言(英语:Structural Query Language,缩写:SQL),是一种特殊目的之编程语言,用于数据库中的标准数据查询语言. 各种通行的数据库系统在其实践过程中都对SQ ...

  10. jQuery(6)——jQuery对表单、表格的操作及更多应用

    jQuery对表单.表格的操作及更多应用 [表单应用] 一个表单有表单标签.表单域及表单按钮三个基本部分. 单行文本框应用:获取和失去焦点改变样式. 也可以用CSS中的伪类选择符来实现,但是IE6并不 ...