C. Vasya and String (尺取法)

 

 

  C. Vasya and String

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

 

理解:尺取法也称作双指针法,我更喜欢称之为追赶法,所谓追赶法呢,就是一个R在前面右移,L在限制K内进行追赶。也就是当R右移到某个位置时不满足K,那么就右移L使 【L,R】区间满足限制K,秉着这样的想法,来更新满足题意的区间。下面表格给出本题的手动模拟过程,就本题而言,还可以采用前缀和。

 

 

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <string>
 5 #include <vector>
 6 #include <map>
 7 #include <set>
 8 #include <list>
 9 #include <deque>
10 #include <queue>
11 #include <stack>
12 #include <cstdlib>
13 #include <cstdio>
14 #include <cmath>
15 #include <iomanip>
16 #define ull unsigned long long
17 #define ll long long
18 #define pb push_back
19 #define rep(i,start,end) for(int i=start;i<=end;i++)
20 #define per(i,end,start) for(int i=end;i>=start;i--)
21 #define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
22 #define lc now<<1
23 #define rc now<<1|1
24 using namespace std;
25 const int mod = 998244353;
26 const int mxn = 1e6 +7;
27 ll _,n,m,t,k,u,v,ans,cnt,ok,lim;
28 ll w[mxn] , cost[mxn] ,  far[mxn] , siz[mxn];
29 string str ;
30 double dp[11][11] ;
31 int main()
32 {
33     while(cin>>n>>k>>str)
34     {
35         char ch = 'a';
36         int l = 0 , ans = 0 , ok = 0 ;
37         for(int i=0;i<str.size();i++)
38         {
39             if(str[i]==ch)
40             {
41                 if(ok<k) ok++; /// 先在限制内一直右移,直到达到最大限制
42                 else    ///将前指针右移一位,保持K个改变的限制
43                 {
44                     while(l<i && str[l]!= ch) l++;
45                     l++;
46                 }
47             }
48             ans = max(ans,i-l+1); /// 不断更新值
49         }
50         l = 0 ; ok = 0 ;ch = 'b' ;
51         for(int i=0;i<str.size();i++)
52         {
53             if(str[i]==ch)
54             {
55                 if(ok<k) ok++;
56                 else
57                 {
58                     while(l<i && str[l]!= ch) l++;
59                     l++;
60                 }
61             }
62             ans = max(ans,i-l+1);
63         }
64         cout<<ans<<endl;
65     }
66 }

 

posted @ 2020-05-06 19:13  __MEET  阅读(204)  评论(0编辑  收藏  举报