双端队列 C. Vasya and String
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?
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.
string s;
int main()
{
int n,k;
while(cin>>n>>k){
cin>>s;
int start = 0;
int ans = 0;
deque<int> De;
for(int i = 0;i < s.size();i ++){
if(s[i] == 'b') De.push_back(i);
if(De.size() > k){
start = De.front()+1;
De.pop_front();
}
ans = max(ans,i-start+1);
}
De.clear();
start = 0;
for(int i = 0;i < s.size();i ++){
if(s[i] == 'a') De.push_back(i);
if(De.size() > k){
start = De.front()+1;
De.pop_front();
}
ans = max(ans,i-start+1);
}
cout<<ans<<endl;
}
return 0;
}
双端队列 C. Vasya and String的更多相关文章
- 补番计划 (长沙理工大学第十一届程序设计竞赛)(双端队列+set容器+string)
补番计划 Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submissi ...
- 双端队列(单调队列)poj2823 区间最小值(RMQ也可以)
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 41844 Accepted: 12384 ...
- P - The Shortest Path in Nya Graph-hdu4725(双端队列+拆点)
题意:有N个点和N层..一层有X个点(0<=X<=N).两邻两层间有一条路花费C.还有M条小路在两个点之间.问从第一个点走到第N个点最短路是多少... 可以考虑在每一层增加一个点,这个点到 ...
- 《算法实战策略》-chaper19-队列、栈和双端队列
对于计算机专业的学生来说,他们一定会很熟悉一句话:程序设计 = 算法 + 数据结构.而根据笔者的理解,所谓程序设计其实就是为了编程解决实际问题,所谓算法是一种解决问题某种思维的方法,但是思维需要得到编 ...
- python 下的数据结构与算法---4:线形数据结构,栈,队列,双端队列,列表
目录: 前言 1:栈 1.1:栈的实现 1.2:栈的应用: 1.2.1:检验数学表达式的括号匹配 1.2.2:将十进制数转化为任意进制 1.2.3:后置表达式的生成及其计算 2:队列 2.1:队列的实 ...
- Java 模拟队列(一般队列、双端队列、优先级队列)
队列: 先进先出,处理类似排队的问题,先排的.先处理,后排的等前面的处理完了,再处理 对于插入和移除操作的时间复杂度都为O(1).从后面插入,从前面移除 双端队列: 即在队列两端都能够insert和r ...
- C++ STL 双端队列deque详解
一.解释 Deque(双端队列)是一种具有队列和栈的性质的数据结构.双端队列的元素可以从两端弹出,其限定插入和删除操作在表的两端进行. 二.常用操作: 1.头文件 #include <deque ...
- [Swift]LeetCode641. 设计循环双端队列 | Design Circular Deque
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
- POJ3662 SPFA//二分 + 双端队列最短路
https://cn.vjudge.net/problem/12427/origin 题意:求1到N第K + 1大条边权最小的路径 首先想到dp递推,dp[x][y]表示到x这个点经过y条免费边的最小 ...
随机推荐
- 《精通并发与Netty》学习笔记(11 - 详解NIO (二) 分散/聚集 Scatter/Gather、Selector)
一.分散/聚集 Scatter/Gather scatter/gather指的在多个缓冲区上实现一个简单的I/O操作,比如从通道中读取数据到多个缓冲区,或从多个缓冲区中写入数据到通道:scatter( ...
- 教你用免费的hihttps开源WEB应用防火墙阻止暴力破解密码
教你用免费的hihttps开源WEB应用防火墙阻止暴力破解密码 很多企业都有自己的网站,需要用户登录后才能访问,但有大量的黑客攻击软件可以暴力破解网站密码,即使破解不了也非常恶心.有没有免费的解决办法 ...
- 子组件props接受父组件传递的值,能不能修改的问题
参考链接:https://www.cnblogs.com/pangchunlei/p/11139356.html
- 《C程序设计语言》学习笔记
1. 在C程序中,如果字符串过长而需要跨行时,要在换行时加上“\”. printf("Hello, world"); // error printf("Hello, wo ...
- [LuoguP2124]奶牛美容_bfs_floyd_曼哈顿距离
奶牛美容 题目链接:https://www.luogu.org/problem/P2124 数据范围:略. 题解: 发现数据范围只有$50$,显然可以直接$bfs$求出联通块,$floyd$求出相邻两 ...
- HTML 全局属性 = HTML5 中添加的属性。
属性 描述 accesskey 规定激活元素的快捷键. class 规定元素的一个或多个类名(引用样式表中的类). contenteditable 规定元素内容是否可编辑. contextmenu 规 ...
- Proxy 和aop
Proxy 就是代理,意思就是 你不用去做,别人代替你去处理 先来个静态代理 public interface Hello { void say(String name); } 被代理类 public ...
- (四)循环队列 VS 数组队列 (效率对比)
目录 背景 测试代码 结果 链表 随机访问 背景 各自完成插入 10万.20万 条随机数,然后再将这些随机数出队列 : 测试代码 /** * 测试速度 */ public String testSpe ...
- C程序设计语言练习 第三章
3.3 else-if语句 折半查找,这里通过一个折半查找说明三路判定程序的用法.该函数用于判定已排序好的数组v中是否存在某个特定的值.数组v的元素必须以升序排列.如果v中包含x,则该函数返回x在v中 ...
- 10.使用du将文件按大小进行排序
按G进行排序du -sh * | grep G | sort -nr