【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;
}

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