zoukankan      html  css  js  c++  java
  • codeforces 676C

    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
    Copy
    4 2
    abba
    output
    4
    input
    Copy
    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和b的字符串,你能够挑出<=k个字母来,让他变化成你想要的字母,问,在此变换下能够得到的最长的连续相同子串的长度是多少?

     分析:

    分两种情况,要连续a和连续b。

    连续a 时,记录前面有多少个b字母,在这个上面进行尺取就行了。

    #include <bits/stdc++.h>
    
    using namespace std;
    
    const int maxn = 100005;
    char str[maxn];
    int numb[maxn];
    int numa[maxn];
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        int n,k;
        scanf("%d%d",&n,&k);
        scanf("%s",str+1);
    
        for(int i = 1; i <= n; i++) {
            if(str[i]=='b')
                numb[i] = numb[i-1] + 1;
            else numb[i] = numb[i-1];
    
            if(str[i]=='a')
                numa[i] = numa[i-1] + 1;
            else numa[i] = numa[i-1];
        }
    
        int L = 1;
        int R = 1;
        int ans = 0;
        int tmp = 0;
        int pos = 0;
        while(R<=n) {
            while(R<=n&&numb[R]-tmp<=k) {
                ans = max(ans,R-pos);
                R++;
            }
            pos++;
            tmp=numb[L];
            L++;
        }
    
        L = 1;R = 1;
        tmp = 0;
        pos = 0;
        while(R<=n) {
            while(R<=n&&numa[R]-tmp<=k) {
                ans = max(ans,R-pos);
                R++;
            }
            pos++;
            tmp=numa[L];
            L++;
        }
    
        printf("%d
    ",ans);
    
    
        return 0;
    }
    View Code

  • 相关阅读:
    [独库骑行之行路难]行路难!
    [独库骑行之我们穿过草原]巴音布鲁克大草原
    [独库骑行之我们路过湖泊]天山的高山湖泊
    [Tips]通过retintolibc方法编写通用exp的一个小技巧
    [独库骑行之奇山异石]丹霞地貌和雅丹地貌
    [独库骑行之我们穿过草原]美丽的乔尔玛草原
    [独库骑行之我们路过沙漠]塔克拉玛干的边缘
    [独库骑行之我们路过森林]那拉提的山林
    大家新年快乐!
    记忆力衰退
  • 原文地址:https://www.cnblogs.com/TreeDream/p/8665965.html
Copyright © 2011-2022 走看看