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

一开始的渣代码运气好到test65才跪……稍微改下形式就行

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x,y) memset(x,y,sizeof(x))
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=100010;
char s[N];
int main(void)
{
int n,i,j,cnt,k;
while (~scanf("%d%d",&n,&k))
{
scanf("%s",s);
int L,R,ma=0,mb=0,c;
L=R=c=0;
while (L<n)
{
while (R<n&&c<=k)
{
if(s[R]=='b')
{
if(c==k)
break;
c++;
}
R++;
}
ma=max(ma,R-L);
L++;
c=c-(s[L-1]=='b');
}
L=R=c=0;
while (L<n)
{
while (R<n&&c<=k)
{
if(s[R]=='a')
{
if(c==k)
break;
c++;
}
R++;
}
mb=max(mb,R-L);
L++;
c=c-(s[L-1]=='a');
}
printf("%d\n",max(ma,mb));
}
return 0;
}

Codeforces Round #354 (Div. 2)——C. Vasya and String(尺取)的更多相关文章

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

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

  3. Codeforces Round #354 (Div. 2) C. Vasya and String

    题目链接: http://codeforces.com/contest/676/problem/C 题解: 把连续的一段压缩成一个数,对新的数组求前缀和,用两个指针从左到右线性扫一遍. 一段值改变一部 ...

  4. Codeforces Round #116 (Div. 2, ACM-ICPC Rules) E. Cubes (尺取)

    题目链接:http://codeforces.com/problemset/problem/180/E 给你n个数,每个数代表一种颜色,给你1到m的m种颜色.最多可以删k个数,问你最长连续相同颜色的序 ...

  5. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  6. 构造水题 Codeforces Round #206 (Div. 2) A. Vasya and Digital Root

    题目传送门 /* 构造水题:对于0的多个位数的NO,对于位数太大的在后面补0,在9×k的范围内的平均的原则 */ #include <cstdio> #include <algori ...

  7. Codeforces Round #184 (Div. 2) E. Playing with String(博弈)

    题目大意 两个人轮流在一个字符串上删掉一个字符,没有字符可删的人输掉游戏 删字符的规则如下: 1. 每次从一个字符串中选取一个字符,它是一个长度至少为 3 的奇回文串的中心 2. 删掉该字符,同时,他 ...

  8. Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和

    Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx ...

  9. 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String

    题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...

随机推荐

  1. uvm_comps.svh

    UVM的文件组织方式很有意思,比如,在src/comps/ 下的所有文件都通过uvm_comps.svh 包含进去. `include "comps/uvm_pair.svh" ` ...

  2. C#语言 数据类型 类型转换

    数据类型有  基本数据类型 和  引用数据类型 两大类型. 数据类型 C#语言 .NET(通用语言) 大小(字节) 值区间 基本数据类型 值类型 整型 不能存在小数点,可以有负数 byte Byte ...

  3. hihoCoder #1068 : RMQ-ST算法(模板)

    AC G++ 826ms 146MB 思路: 时间复杂度O(nlogn). //#include <bits/stdc++.h> #include <iostream> #in ...

  4. 洛谷 P1732 活蹦乱跳的香穗子

    题目描述 香穗子在田野上调蘑菇!她跳啊跳,发现自己很无聊,于是她想了一个有趣的事情,每个格子最多只能经过1次,且每个格子都有其价值 跳的规则是这样的,香穗子可以向上下左右四个方向跳到相邻的格子,并且她 ...

  5. HITICS || 2018大作业 程序人生 Hello's P2P

    摘  要 本文通过分析一个hello.c的完整的生命周期,从它开始被编译,到被汇编.链接.在进程中运行,讲解了Linux计算机系统执行一个程序的完整过程. 关键词:操作系统,进程,程序的生命周期 目 ...

  6. 响应式Web设计- Viewport

    什么是Viewport? viewport是用户网页的可视区域, 翻译为中文可以叫做"视区". 设置Viewport 一个常用的针对移动网页优化过的页面的Viewport meta ...

  7. Shell脚本调用SQL文格式

    Shell脚本调用SQL文格式 1. 定义需要执行的SQL文,以及需要输出文件 OUTFILE=\${DATADIR}/\${FILENAME} SQLFILE=\${DATADIR}/check_t ...

  8. CS193p Lecture 10 - Multithreating, UIScrollView

    Multithreating(多线程) 网络请求例子: NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithStrin ...

  9. ios之coredata(二)

    上面一篇文章介绍了coredata的有关基本概念,由于大部分是参考别人文章中的内容,所以感觉有点虚,而且估计也是比较难以理解,下面这篇文章通俗一点说说学习coredata后的一些理解,然后给出一个简单 ...

  10. 移动网页端HTML5 meta便签

    width = device-width:标识宽度是设备屏幕的宽度 initial-scale = 1.0 :标识初始的缩放比例 minimum-scale =0.5 :表示最小的缩放比例 maxim ...