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 ;
}

最新文章

  1. 关于js单线程(转载)
  2. 元组tuple
  3. iOS @try @catch简单应用举例
  4. PHP自定义函数使用外部变量
  5. 在非MFC程序中引用CString
  6. 【转】补充说明:关于Beaglebone black上debian无图形界面的问题及QT的窗口示例
  7. 关于css样式line-height的应用
  8. poj 2531 Network Saboteur(经典dfs)
  9. mac下使用命令行打包出现bash gradle command not found的解决方案
  10. Material Theme 文件名的标签(tab)被大写了
  11. C++对象模型(二):The Semantics of Copy Constructors(拷贝构造函数之编译背后的行为)
  12. 居中 html css
  13. [error] - Build path is incomplete. Cannot find class file for org/aspectj/weaver/refl
  14. selenium模拟登陆淘宝
  15. wildfly 10上使用最新的 Hibernate ORM OGM
  16. bzoj 2186
  17. saltstack学习之一:服务架构以及相关配置安装运行
  18. Bootstrap插件概述
  19. MongoDB学习笔记(一)安装配置
  20. asp.net错误记录

热门文章

  1. IDEA Tomcat部署时war和war exploded区别以及平时踩得坑
  2. javascript飞机大战-----005创建子弹对象2
  3. ora-28056错误解决
  4. Andrew Ng机器学习公开课笔记 -- Generative Learning algorithms
  5. sql server学习路径地址
  6. OpenS-CAD学习(1)
  7. 《TP5.0学习笔记---模型篇》
  8. Mybatis怎么在mapper中用多个参数
  9. sdut 迷之容器(线段树+离散化)
  10. oracle 死锁查询及处理