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?
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
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个字符 输出连续相同字符的最大长度
题解:补起来 还是暴力题
以某一个位置为起点 计算最大值
之后反转所有的字母 重复fun();
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<cmath> 5 #include<queue> 6 #include<stack> 7 #include<map> 8 #define ll __int64 9 #define pi acos(-1.0) 10 using namespace std; 11 int n,k; 12 char a[100005]; 13 int ans; 14 void fun() 15 { 16 int gg=1,exm=0; 17 for(int i=1;i<=n;i++) 18 { 19 while(gg<=n&&(a[gg]=='a'||exm<k))//gg存下 能到达的最远位置,并且以i+1为起点 20 { // 最少也能到达 gg 21 if(a[gg]=='b') 22 exm++; 23 gg++; 24 } 25 // cout<<gg<<endl; 26 ans=max(ans,gg-i); 27 if(a[i]=='b') // 除去 对i+1位置为起点的影响 28 exm--; 29 } 30 } 31 int main() 32 { 33 scanf("%d %d",&n,&k); 34 getchar(); 35 for(int i=1;i<=n;i++) 36 scanf("%c",&a[i]); 37 ans=0; 38 fun(); 39 for(int i=1;i<=n;i++) 40 { 41 if(a[i]=='a') 42 a[i]='b'; 43 else 44 a[i]='a'; 45 } 46 fun(); 47 cout<<ans<<endl; 48 return 0; 49 }