Codeforces Round #354 (Div. 2) C. Vasya and String

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

 

 解题思路 :

 维护前缀和。枚举每一个位置,二分查找能够延续的最远距离。

(1)

a->0,b->1

如样例     aabaabaa    n=8  k=2

变成01序列     00100100      相当于把b替换成a

(2)同理a->1  b->0。就相当与a替换成b

/* ***********************************************
Author        :guanjun
Created Time  :2016/5/26 15:30:33
File Name     :cf354c.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 100010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
    int x,y;
};
struct cmp{
    bool operator()(Node a,Node b){
        if(a.x==b.x) return a.y> b.y;
        return a.x>b.x;
    }
};

bool cmp(int a,int b){
    return a>b;
}
int a[maxn];
int b[maxn];
int sum[maxn];
int n,k;
int solve(int *sum){
    int len=-1;
    for(int i=1;i<=n;i++){
        int x=upper_bound(sum+1,sum+1+n,sum[i-1]+k)-sum;
        x--;
        if(x-i+1>len)len=x-i+1;
    }
    return len;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    char s[maxn];
    while(cin>>n>>k){
        scanf("%s",s+1);
        cle(sum);
        cle(a),cle(b);
        for(int i=1;i<=n;i++){
            if(s[i]=='a')a[i]=1;
            else b[i]=1;
        }
        for(int i=1;i<=n;i++){
            sum[i]=sum[i-1]+b[i];
        }
        int x=solve(sum);
        for(int i=1;i<=n;i++){
            sum[i]=sum[i-1]+a[i];
        }
        int y=solve(sum);
        cout<<max(x,y)<<endl;
    }
    return 0;
}

 

posted on 2016-05-26 16:42  Beserious  阅读(187)  评论(0编辑  收藏  举报