• CodeForces


    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
    4 2
    abba
    
    Output
    4
    
    Input
    8 1
    aabaabaa
    
    Output
    5

    题目链接:http://codeforces.com/problemset/problem/676/C

    题目大意: 给一个字符串由‘a', 'b'组成,给两个数字,一个数字是字符串的长度n,另一个是一个数字k,可以改变多少次字母(每次改一个), 改的目的是为了使字符串的相同字母组成的字串是多少。如aabaabaa, 改变一次,改变第一个b得到字符串aaaaabaa,连续相同字母组成的字串为5, 改变第二个为aabaaaaa也是5,于是输出5.  

    注意:不仅可以改变b, 还可以改变a。 

    ac代码:

    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<iostream>
    #include<cstdlib>
    #include<string>
    using namespace std;
    const int MaxN = 1e5 + 7;
    char a[MaxN];  
    int b[MaxN], c[MaxN];
    int n, k, p = 0, q = 0;
    int main()
    {
        int ans1 = 0, ans2 = 0, ans = 0;  //ans1 为改变b的最长长度 , ans2为改变a的, ans为max(ans1, ans2)
        cin >> n >> k;
        for(int i = 0; i < n; i++) {
            cin >> a[i];
            if(a[i] == 'a') b[++p] = i;  // p为有多少个a,每个a的位置在什么地方
            else c[++q] = i;   //q为有多少个b,b在什么地方
        }
        if(k >= p || k >= q) printf("%d
    ", n);
        else {
            ans1 = b[k + 1];
            for(int i = 1; k + i < p; i++)   
                ans1 = max(ans1, b[k + i + 1] - b[i] - 1);  
            ans1 = max(ans1, n - b[p - k] - 1);  //最后一段需要用总长度判断
            ans2 = c[k + 1];
            for(int i = 1; k + i < q; i++) 
                ans2 = max(ans2, c[k + i + 1] - c[i] - 1);
            ans2 = max(ans2, n - c[q - k] - 1);
            ans = max(ans1, ans2);
            printf("%d
    ",ans);
        }
    }
  • 相关阅读:
    [转]The Regular Expression Object Model
    [转]Blue Prism Interview Questions and Answers
    [转]Date and String Function in BluePrism
    [转]Have a query in Blue prism coding stage and collection stage.
    [转]Blue Prism VBO Cheat Sheet
    [转]秒杀系统优化方案之缓存、队列、锁设计思路
    [转]How to: Create a Report Server Database (Reporting Services Configuration)
    [转]Python in Visual Studio Code
    [转]How to Download and Setup Blue Prism
    [转]Blue Prism Architecture
  • 原文地址:https://www.cnblogs.com/smuzoey/p/11787450.html
Copyright © 2020-2023  润新知