【CodeForces 676C 】 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”.

题意:给一个只含ab的字符串,问最多改变k次的情况下,能得到的最大连续a或b的长度

思路:

和前面一场的这个题几乎一模一样的 传送门

因为越往后可以留给我们来改变的字符就越来越多,满足了一定的单调性(下标->可改变值,单增),所以可以用二分。主要思路就是外层遍历一遍窗口大小,右端点每次都固定在n,左端点为遍历的i,然后看这个窗口区间下的最优解,即是内层为一个二分,每次看【L,mid】之间的要改掉的a(或者b)的个数是否小于等于k,是的话目前可以得到的答案就是mid-L+1,同时左端点L = mid + 1,将下一步mid右移。 反之则R = mid -1, 缩小区间范围。 每次得到的答案和ans取小就行了。

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

string s;
ll sum1[maxn];
ll sum2[maxn];
ll n, k;

ll cal(ll  (&sum)[maxn] )
{
    ll ans = 0;
    rep(i,1,n)
    {
        ll L = i, R = n;
        while(L<=R)
        {
            ll mid = (L+R)>>1;
            if(sum[mid]-sum[i-1]<=k)
            {
                ans = max(mid-i+1,ans);
                L = mid + 1;
            }
            else R = mid - 1;
        }
    }
    return ans;
    //return 0;
}

int main()
{
    while(cin>>n>>k)
    {
        cin>>s;
        sum1[0] = sum2[0] = 0;
        for(int i=0;i<s.size();i++) sum1[i+1] = sum1[i] + !(s[i] - 'a') ;
        for(int i=0;i<s.size();i++) sum2[i+1] = sum2[i] + (s[i] - 'a');
        ll a = cal(sum1);
        ll b = cal(sum2);
        cout<<max(a,b)<<'\n';
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章