题目链接:

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

题意:

问最多改变k个字母才能使相同字母组成的子串最长;

思路:

最长的那个字串可以是a组成的,也可能是b组成的,现在枚举最长的子串的左端点,二分右端点,找到最长的长度,对于a,b两种情况都这样处理;用尺取法也可以搞;

AC代码:
#include <bits/stdc++.h>
/*
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
*/
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=1e5+;
int n,sum[N],k;
char s[N];
int check(int x,int y)
{
if(sum[x]-sum[y]<=k)return ;
return ;
}
int main()
{
scanf("%d%d",&n,&k);
scanf("%s",s);
sum[]=;
for(int i=;i<n;i++)
{
if(s[i]=='a')sum[i+]=sum[i];
else sum[i+]=sum[i]+;
}
int ans=;
for(int i=;i<=n;i++)
{
int l=i,r=n;
while(l<=r)
{
int mid=(l+r)>>;
if(check(mid,i-))l=mid+;
else r=mid-;
}
ans=max(ans,l-i);
}
for(int i=;i<n;i++)
{
if(s[i]=='b')sum[i+]=sum[i];
else sum[i+]=sum[i]+;
}
for(int i=;i<=n;i++)
{
int l=i,r=n;
while(l<=r)
{
int mid=(l+r)>>;
if(check(mid,i-))l=mid+;
else r=mid-;
}
ans=max(ans,l-i);
}
cout<<ans<<"\n";
return ;
}

codeforces 676C C. Vasya and String(二分)的更多相关文章

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

  2. codeforces 354 div2 C Vasya and String 前缀和

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

  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. Vasya and String(尺取法)

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

  6. C. Vasya and String

    原题链接 C. Vasya and String High school student Vasya got a string of length n as a birthday present. T ...

  7. codeforces 676C

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

  8. Codeforces 676C Vasya and String(尺取法)

    题目大概说给一个由a和b组成的字符串,最多能改变其中的k个字符,问通过改变能得到的最长连续且相同的字符串是多长. 用尺取法,改变成a和改变成b分别做一次:双指针i和j,j不停++,然后如果遇到需要改变 ...

  9. Codeforces 1073C:Vasya and Robot(二分)

    C. Vasya and Robot time limit per test: 1 secondmemory limit per test: 256 megabytesinput: standard ...

随机推荐

  1. sql表设计器的几个默认值

    sql表设计器的几个默认值: 空字符串‘’(注意是单引号) 当前时间getdate() 逻辑值0或1 汉字或英文字符串需在前面加大写N,并用单引号引起如: N'已发货'

  2. VS2010 Web网站发布详解

    1.项目某一网站 右键 发布 出来对话框后 发布方法选择文件系统,发布前删除所有现有文件(选择此项) ,然后点击发布就OK了. 2.之后如果只修改aspx页面,编译下无需再次发布,如果修改了类库或.c ...

  3. 导入flash 逐帧动画

    只要图片是一序列的,比如0001-0100,就可以使用导入素材功能,选择第一张图片,选中影片剪辑并编辑然后点击导入到舞台,然后FlashCS6工具就会问你,这是一序列图片,是否逐帧导入,点是,就ok了 ...

  4. PostgreSQL的AnynonArray的例子

    程序: CREATE OR REPLACE FUNCTION kappend(anynonarray, anyelement) RETURNS text AS $$ ; $$ LANGUAGE SQL ...

  5. iOS10适配知识点

    http://ios.jobbole.com/89551/ http://ios.jobbole.com/88982/ 2.隐私数据访问问题 问题出现 现在app能运行了,当我打开相机时突然又cras ...

  6. 关于C#资源文件操作的总结

    // 在这里,我来总结一下关于资源文件的相关操作. //1. 比较常见的有获取资源文件对应的文件流,然后转换到相对应的文件 // 比较典型的做法是通过代码程序集加载指定资源 // 如下通过Assemb ...

  7. php实现工厂模式

    设计模式-使用php实现工厂方法模式 [概要] 创建型模式 定义一个用于创建对象的接口,让子类决定实例化哪一个类.Factory Method使用一个类的实例化延迟到其子类[GOF95] [结构图] ...

  8. 强大的ASP.NET控件---验证控件

        学习完了牛腩之后,在进行ASP.NET的学习的时候,对全部学的知识.都有一种似曾相识的感觉,"哦,这个,在牛腩新闻公布系统中用过".仅仅只是那时候.用的也是迷迷糊糊的,就说 ...

  9. php & 引用

    引用的作用: 如果程序比较大,引用同一个对象的变量比较多,并且希望用完该对象后手工清除它,个人建议用 "&" 方式,然后用$var=null的方式清除. 其它时候还是用ph ...

  10. Caching Best Practices--reference

    reference:http://java.dzone.com/articles/caching-best-practices There is an irresistible attraction ...