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?
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.
Print the only integer — the maximum beauty of the string Vasya can achieve by changing no more than k characters.
4 2 abba
4
8 1 aabaabaa
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;
}