当前位置 : 主页 > 编程语言 > c语言 >

Codeforces Round #354 (Div. 2) C. Vasya and String deque 双端队列

来源:互联网 收集:自由互联 发布时间:2023-09-07
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

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
inputCopy
4 2
abba
outputCopy
4
inputCopy
8 1
aabaabaa
outputCopy
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”.

双端队列的巧妙应用:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 500005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-7
#define pll pair<ll,ll>



ll quickpow(ll a, ll b) {
    ll ans = 1;
    a = a % mod;
    while (b > 0) {
        if (b % 2)ans = ans * a;
        b = b / 2;
        a = a * a;
    }
    return ans;
}

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}



int main()
{
    ios::sync_with_stdio(false);
    string s;

    int k;
    int n; cin >> n;
    cin >> k;
    cin >> s;
    int start = 0;
    int ans = 0;
    deque<int> de;
    for (int i = 0; i < (s.size()); i++) {
        if (s[i] == 'b')
            de.push_back(i);
        if ((de.size()) > k) {
            start = de.front() + 1;
            de.pop_front();
        }
        ans = max(ans, i - start + 1);
        //cout << ans << endl;
    }
    de.clear();
    start = 0;
    for (int i = 0; i < (s.size()); i++) {
        if (s[i] == 'a')
            de.push_back(i);
        if ((de.size()) > k) {
            start = de.front() + 1;
            de.pop_front();
        }
        ans = max(ans, i - start + 1);
    }
    cout << ans << endl;
}
上一篇:scu 优先队列
下一篇:没有了
网友评论