题目大概说给一个由a和b组成的字符串,最多能改变其中的k个字符,问通过改变能得到的最长连续且相同的字符串是多长。

用尺取法,改变成a和改变成b分别做一次:双指针i和j,j不停++,然后如果遇到需要改变且改变次数用完就让i++更正改变次数,最后更新答案。时间复杂度O(n)。

另外,注意到k=0的情况。

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char str[];
int main(){
int n,k;
scanf("%d%d%s",&n,&k,str);
int i=,j=,tmpn=,tmpk=,ans=;
while(j<n){
if(str[j]=='a'){
++tmpn;
}else if(k==){
tmpn=;
}else{
while(tmpk==k){
if(str[i]=='b'){
--tmpk;
}
--tmpn;
++i;
}
++tmpk;
++tmpn;
}
ans=max(ans,tmpn);
++j;
}
i=; j=; tmpn=; tmpk=;
while(j<n){
if(str[j]=='b'){
++tmpn;
}else if(k==){
tmpn=;
}else{
while(tmpk==k){
if(str[i]=='a'){
--tmpk;
}
--tmpn;
++i;
}
++tmpk;
++tmpn;
}
ans=max(ans,tmpn);
++j;
}
printf("%d",ans);
return ;
}

Codeforces 676C Vasya and String(尺取法)的更多相关文章

  1. Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)

    题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...

  2. CodeForces 701C They Are Everywhere 尺取法

    简单的尺取法…… 先找到右边界 然后在已经有了所有字母后减小左边界…… 不断优化最短区间就好了~ #include<stdio.h> #include<string.h> #d ...

  3. hdu 5672 String 尺取法

    String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem D ...

  4. codeforces #364c They Are Everywhere 尺取法

    C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. HDU 5672 String 尺取法追赶法

    String Problem Description There is a string S.S only contain lower case English character.(10≤lengt ...

  6. codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)

    题目链接: C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  7. Codeforces 660C Hard Process(尺取法)

    题目大概说给一个由01组成的序列,要求最多把k个0改成1使得连续的1的个数最多,输出一种方案. 和CF 676C相似. #include<cstdio> #include<algor ...

  8. Codeforces 650B Image Preview(尺取法)

    题目大概说手机有n张照片.通过左滑或者右滑循环切换照片,滑动需要花费a时间:看一张照片要1时间,而看过的可以马上跳过不用花时间,没看过的不能跳过:有些照片要横着看,要花b时间旋转方向.那么问T时间下最 ...

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

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

随机推荐

  1. October 12th 2016 Week 42nd Wednesday

    Passion is momentary; love is enduring. 激情短暂,真爱长久. What is love? And what is real love? We are alway ...

  2. MongoDB csv文件导入导出

    1.导出到csv文件: 2.从csv导入: 数据经过csv导出导入,有一个非常隐蔽的问题,编写代码时需要注意: 先导入一条数据: 其中Price是double类型: 然后我把该条记录导出到Demo.c ...

  3. codevs 1080 线段树练习

    链接:http://codevs.cn/problem/1080/ 先用树状数组水一发,再用线段树水一发 树状数组代码:84ms #include<cstdio> #include< ...

  4. MVC - 11(下)jquery.tmpl.js +ajax分页

    继续 mvc-11(上).dto:http://www.cnblogs.com/tangge/p/3840060.html jquery.tmpl.js 下载:http://pan.baidu.com ...

  5. python装饰器入门

    按别人的教程弄的. 要清楚基于类和基于函数的实现的不同之处. #!/usr/bin/env python # -*- coding: utf-8 -*- ''' class entryExit(obj ...

  6. 针对较大基数的排列组合算法Java实现类(n选m)

    package com.utils; import java.math.BigDecimal; import java.math.RoundingMode; public class PLZUUtil ...

  7. Freemarker使用入门

    一.概述: FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写 Template + data_model = output FreeMarker也是与Web容 ...

  8. 用Node.js开发Windows 10物联网应用

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 未来10年内,物联网将会如移动互联网这样深入到我们生活的各方各面.所以微软现在对物联网进行了 ...

  9. Linux系统安装及初始化(ubuntu14.04)

    Windows 7下硬盘安装Ubuntu 14.04图文教程 Ubuntu 官方已经发布了正式版的 Ubuntu 14.04 LTS,并宣称这是为云计算准备的版本.该版本在云平台和伸缩环境的可靠性.性 ...

  10. SSH Key连接github提示Permission denied (publickey).错误

    root@debian64:/home/xiaoliuzi/.ssh/key_backup# ssh -T git@github.com The authenticity of host 'githu ...