您的位置:首页 > 其它

Codeforces Round #354 (Div. 2)(C)尺取

2016-05-26 10:31 260 查看
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 kcharacters.

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

题意:你有长度为n的字符串,字符串只有字符a,b组成,现在你可以任意改k个字母,问最长的连续子串有多长?

题解:这里我们可以直接考虑使用尺取法,字母a,b分别跑一遍,找出最大的答案

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;

#define N int(1e5+10)
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
typedef long long LL;

#ifdef CDZSC
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif

char s
;

int main()
{
#ifdef CDZSC
freopen("i.txt", "r", stdin);
//freopen("o.txt","w",stdout);
int _time_jc = clock();
#endif
//ios::sync_with_stdio(false);
int n,k;
while (~scanf("%d%d%s", &n,&k,s))
{
int ans = 0;
int cost = k;
int L = 0, R=0;
while (L < n&&R<n)
{
while (cost>=0&&R<n)
{
if (s[R] == 'b'&&!cost)break;
if (s[R] == 'b')
{
cost--;
}
ans = max(R - L + 1, ans);
R++;
}

if (s[L] == 'b')cost++;
L++;
}

L = 0, R = 0;
cost = k;
while (L < n&&R<n)
{
while (cost>=0 && R<n)
{
if (s[R] == 'a'&&!cost)break;
if (s[R] == 'a')cost--;
ans = max(R - L + 1, ans);
R++;
}
if (s[L] == 'a')
cost++;
L++;
}
printf("%d\n", ans);
}
#ifdef CDZSC
debug("time: %d\n", int(clock() - _time_jc));
#endif
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: