zoukankan      html  css  js  c++  java
  • Codeforces Round #354 (Div. 2) C

    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
    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".

    题意:长度为n的字符串 只有‘a’,‘b’ 组成  只能改变k个字符 输出连续相同字符的最大长度

    题解:补起来 还是暴力题

            以某一个位置为起点 计算最大值

            之后反转所有的字母 重复fun();

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<cmath>
     5 #include<queue>
     6 #include<stack>
     7 #include<map>
     8 #define ll __int64
     9 #define pi acos(-1.0) 
    10 using namespace std;
    11 int n,k;
    12 char a[100005];
    13 int ans;
    14 void  fun()
    15 {
    16     int gg=1,exm=0;
    17     for(int i=1;i<=n;i++)
    18     {
    19         while(gg<=n&&(a[gg]=='a'||exm<k))//gg存下 能到达的最远位置,并且以i+1为起点 
    20         {                                // 最少也能到达 gg
    21             if(a[gg]=='b')
    22             exm++;
    23             gg++;
    24         }
    25     //    cout<<gg<<endl;
    26         ans=max(ans,gg-i);
    27         if(a[i]=='b') // 除去 对i+1位置为起点的影响
    28          exm--;
    29     }
    30 }
    31 int main()
    32 {
    33      scanf("%d %d",&n,&k);
    34      getchar();
    35      for(int i=1;i<=n;i++)
    36      scanf("%c",&a[i]);
    37      ans=0;
    38      fun();
    39      for(int i=1;i<=n;i++)
    40      {
    41          if(a[i]=='a')
    42          a[i]='b';
    43          else
    44          a[i]='a';
    45      } 
    46      fun();
    47      cout<<ans<<endl;
    48     return 0;
    49 }
  • 相关阅读:
    【凸优化】【长链剖分】【2019冬令营模拟1.8】tree
    2019冬令营集训1月7-10日总结
    item()方法遍历字典
    contiue和break的用法
    内存地址与变量值的关系
    Python垃圾回收机制
    Python的小整数池
    常量的命名和注意事项
    基本运算符
    Python中最简单快捷的输出方式
  • 原文地址:https://www.cnblogs.com/hsd-/p/5536303.html
Copyright © 2011-2022 走看看