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 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”.
题意:一个只有
a
和
思路:利用尺取的方法,截取在
ac代码:
/* ***********************************************
Author : AnICoo1
Created Time : 2016-08-22-14.17 Monday
File Name : D:\MyCode\2016-8月\2016-8-22.cpp
LANGUAGE : C++
Copyright 2016 clh All Rights Reserved
************************************************ */
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define LL long long
#define ll __int64
#define mem(x,y) memset(x,(y),sizeof(x))
#define PI acos(-1)
#define gn (sqrt(5.0)+1)/2
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
const ll INF=1e9+10;
const int MAXN=1e6+10;
const int MOD=1e9+7;
//head
char str[MAXN];
int main()
{
int n,k;cin>>n>>k;
scanf("%s",str);
int l=0,r=0,ans=0,cnt=0;
for(;r<n;)
{
while(r<n&&cnt<=k)
{
if(str[r]=='b')
cnt++;
r++;
}
if(cnt>k) cnt--,r--;
ans=max(ans,r-l);
while(str[l]=='a'&&l<=r)
l++;
l++;
cnt--;
}
l=0,r=0,cnt=0;
for(;r<n;)
{
while(r<n&&cnt<=k)
{
if(str[r]=='a')
cnt++;
r++;
}
if(cnt>k) cnt--,r--;
ans=max(ans,r-l);
while(str[l]=='b'&&l<=r)
l++;
l++;
cnt--;
}
printf("%d\n",ans);
return 0;
}