Codeforces Round #354 (Div. 2)-C
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?
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.
Print the only integer — the maximum beauty of the string Vasya can achieve by changing no more than k characters.
4 2
abba
4
8 1
aabaabaa
5
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个位置的字符[a->b/ b->a],问包含相同字符的连续子串的长度最长是多少?
思路:滑动窗口。定义L,R下标,当没满足k个修改时,R向右滑,当满足K个时记录最优值然后L向右滑。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<bitset>
using namespace std;
#define INF 0x3f3f3f3f
#define PI 3.14159
const int MAXN=+;
char str[MAXN];
int main()
{
#ifdef kirito
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int n,k;
while(~scanf("%d%d",&n,&k)){
scanf("%s",str);
int tota=,totb=;
int L=,R=-,MAXL=-;
while(R+<n)
{
if(min(tota,totb)<=k)
{
R++;
if(str[R]=='a'?tota++:totb++);
if(min(tota,totb)<=k)
{
MAXL=max(MAXL,R-L+);
}
}
else
{
if(str[L]=='a'?tota--:totb--);
L++;
}
}
printf("%d\n",MAXL);
}
return ;
}
Codeforces Round #354 (Div. 2)-C的更多相关文章
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #354 (Div. 2)-D
D. Theseus and labyrinth 题目链接:http://codeforces.com/contest/676/problem/D Theseus has just arrived t ...
- Codeforces Round #354 (Div. 2)-B
B. Pyramid of Glasses 题目链接:http://codeforces.com/contest/676/problem/B Mary has just graduated from ...
- Codeforces Round #354 (Div. 2)-A
A. Nicholas and Permutation 题目链接:http://codeforces.com/contest/676/problem/A Nicholas has an array a ...
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth
题目链接: http://codeforces.com/contest/676/problem/D 题意: 如果两个相邻的格子都有对应朝向的门,则可以从一个格子到另一个格子,给你初始坐标xt,yt,终 ...
- Codeforces Round #354 (Div. 2) C. Vasya and String
题目链接: http://codeforces.com/contest/676/problem/C 题解: 把连续的一段压缩成一个数,对新的数组求前缀和,用两个指针从左到右线性扫一遍. 一段值改变一部 ...
- Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)
题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...
- Codeforces Round #354 (Div. 2) E. The Last Fight Between Human and AI 数学
E. The Last Fight Between Human and AI 题目连接: http://codeforces.com/contest/676/problem/E Description ...
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth bfs
D. Theseus and labyrinth 题目连接: http://www.codeforces.com/contest/676/problem/D Description Theseus h ...
随机推荐
- JQuery 鼠标事件简介
mouseover事件于用户把鼠标从一个元素移动到另外一个元素上时触发,mouseout事件于用户把鼠标移出一个元素时触发. 下面为你详细介绍下jquery中的鼠标事件: (1):click事件:cl ...
- 【剑指offer】题目36 数组中的逆序对
数组中任取两个数字,如果前面的数字大于后面的数字称为一个逆序对 如:1,2,1,2,1 有3个逆序对 思路:知道O(N2)肯定是错的.开始想hash,试图找到O(n)的算法,想了很久,找不到.后来想到 ...
- 【leetcode】Excel Sheet Column Title & Excel Sheet Column Number (easy)
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...
- XML 数据请求与JSON 数据请求
(1)XML 数据请求 使用 AFNetworking 中的 AFHTTPRequestOperation 和 AFXMLParserResponseSerializer,另外结合第三方框架 XMLD ...
- 用 MyEclipse 开发 Spring 入门操作
何为Spring Spring框架是一个轻量级的控制反转(IOC)技术和面向切面编程(AOP)技术的容器框架,利用Spring框架可以实现对象的生命周期管理和分离应用系统中的业务逻辑组件和通用的技术服 ...
- C#冒泡排序
C#最简单的冒泡排序,需要的朋友可作参考: 思路: 使用两个for循环,就可以遍历数组,这样就可以确保每个数组元素都被使用 对比前后两个数,将小的数字和大的交换位置,引入一个临时变量temp来进行交换 ...
- Android 实现类似微信客户端朋友圈更新提示的小红点&栏目订阅
用到的类: com.jauker.widget.BadgeView 实现代码: BadgeView imageBadge = new BadgeView(getContext()); imageBad ...
- 警告 - no rule to process file 'WRP_CollectionView/README.md' of type net.daringfireball.markdown for architecture i386
warning: no rule to process file '/Users/mac/Downloads/Demo/Self/WRP_CollectionView/WRP_CollectionVi ...
- NYOJ题目816它合法吗?
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtIAAAJ0CAIAAACwTVMOAAAgAElEQVR4nO3du1LjzNo24O8kyDkQYh
- PHP之MVC项目实战(二)
本文主要包括以下内容 GD库图片操作 利用GD库实现验证码 文件上传 缩略图 水印 GD库图片操作 <?php $img = imagecreatetruecolor(500, 300); // ...