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 denotesbeauty 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
题意:一个只由'a','b'组成的字符串,长度为n,可以改变k个,求连续最长;
思路:找出连续a的长度,包括0,同样的b也是,求前缀和,求出以k为长度最长的子串;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 999999999
#define pi 4*atan(1)
//#pragma comment(linker, "/STACK:102400000,102400000")
int flaga[],jia;
int flagb[],jib;
int disa[];
int disb[];
int suma[];
int sumb[];
char a[];
int main()
{
int x,y,z,i,t;
scanf("%d%d",&x,&y);
scanf("%s",a+);
flaga[]=;
jia=;
flagb[]=;
jib=;
for(i=;i<=x;i++)
{
if(a[i]=='a')
flaga[jia++]=i;
else
flagb[jib++]=i;
}
flaga[jia++]=x+;
flagb[jib++]=x+;
for(i=;i<jib;i++)
disa[i]=flagb[i]-flagb[i-]-;
for(i=;i<jia;i++)
disb[i]=flaga[i]-flaga[i-]-;
for(i=;i<=;i++)
suma[i]+=suma[i-]+disa[i];
for(i=;i<=;i++)
sumb[i]+=sumb[i-]+disb[i];
int ans=;
for(i=;i<jia;i++)
ans=max(ans,sumb[i+y]-sumb[i-]);
int ans1=;
for(i=;i<jib;i++)
ans1=max(ans1,suma[i+y]-suma[i-]);
if(min(jia,jib)-<=y)
printf("%d\n",x);
else
{
printf("%d\n",max(ans1,ans)+y);
}
return ;
}

codeforces 354 div2 C Vasya and String 前缀和的更多相关文章

  1. codeforces 354 DIV2

    B - Pyramid of Glasses n层杯子,问k分钟能流满多少个杯子?和到香槟一样的过程? 思路:应为水的流速为每分钟一立方体(YY),可以做个转化,把最上层的杯子最原始的容积看成K,每个 ...

  2. Codeforces Round #354 (Div. 2) C. Vasya and String 二分

    C. Vasya and String 题目连接: http://www.codeforces.com/contest/676/problem/C Description High school st ...

  3. Codeforces Round #354 (Div. 2)——C. Vasya and String(尺取)

    C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. Codeforces Round #354 (Div. 2)-C. Vasya and String,区间dp问题,好几次cf都有这种题,看来的好好学学;

    C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  6. codeforces 676C C. Vasya and String(二分)

    题目链接: C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input sta ...

  7. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  8. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  9. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

随机推荐

  1. OC开发_Storyboard——block和动画

     一.协议 @optional :可选的 @requied :必须实现的  二.block 代码块 1. 以一个^开头,然后是参数,然后是一个大括号,包含我们的代码块 [aDictionary enu ...

  2. map容器结构体离散化

    小数坐标离散化: #include"string.h" #include"stdio.h" #include"iostream" #incl ...

  3. 最小树形图(hdu4009)

    Transfer water Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) T ...

  4. spring+springMVC+Mybatis 中使用@Transcational方式管理事务的配置方法

    springMVC 中,事务通常都在service层控制,当然controller层也可以用事务,只要配置配对,但通常不建议直接在controller层配事务,controller的作用是管理参数以及 ...

  5. os模块os.walk() 方法和os.path.join()的简单使用

    os.walk:   http://www.runoob.com/python/os-walk.html os.path.join:   https://blog.csdn.net/zmdzbzbhs ...

  6. JAVA参数没有引用传递,只有值传递

    原文章地址:http://www.cnblogs.com/clara/archive/2011/09/17/2179493.html 当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性, ...

  7. 【Python】Python 打印和输出更多用法。

    Python 打印和输出 简述 在编程实践中,print 的使用频率非常高,特别是程序运行到某个时刻,要检测产生的结果时,必须用 print 来打印输出. 关于 print 函数,前面很多地方已经提及 ...

  8. html基础之css标签

    css style: 里面的写的就叫做css,每一个样式的间隔用英文分号, 全部相同的时候引用class. css有三种写法: 1.在head标签中增加style标签,在style标签中去写css样式 ...

  9. 利用Flash XMLSocket实现”服务器推”技术

    利用Flash XML Socket实现”服务器推”技术的基础是:1.Flash提供了XMLSocket类,服务器利用Socket向Flash发送数据:2.JavaScript和Flash的紧密结合, ...

  10. 泰德激光打标软件 包含 #include "Main.h" 时 原本正确的单元却报错

    问题:泰德激光打标软件  ,当新增单元需要包含 #include "Main.h" 时, 原本正确的单元却报错. 办法:包含 #include "Main.h" ...