您的位置:首页 > 其它

Codeforces 676C Vasya and String 尺取法

2016-06-06 21:20 141 查看
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


题意:给你一个长度为n字符串,然后通过k次操作(a->b或者b->a).问你能得到最长连续相同的串的长度;

思路:直接尺取就好;

代码:

#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 maxn int(1e5+50)
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
#define PI acos(-1.0)
typedef long long ll;
char s[maxn];
int main()
{
#ifdef CDZSC_June
freopen("t.txt", "r", stdin);
#endif
int n,m,suma,sumb;
while(~scanf("%d%d",&n,&m))
{
suma = sumb = 0;//suma表示a的数量,sumb表示b的数量
scanf("%s",s);
int L =0 ,R = 0;
int max1 = 0;
while(1)
{
while(R < n && min(suma,sumb) <= m){
if(s[R] == 'a') suma++;
if(s[R] == 'b') sumb++;
R++;
}
if(min(suma,sumb) <= m) break;
max1 = max(max1,R- L-1);
while(min(suma,sumb) > m){
if(s[L] == 'a') suma--;
if(s[L] == 'b') sumb--;
L++;
}
}
max1 = max(max1,R- L);
printf("%d\n",max1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: