cf676c Vasya and String 尺取法裸题

C. Vasya and String (尺取法) C. Vasya and String 题目链接 大致题意: 给出一串只有a和b构成的字符串,字符串长度为n,可修改字符限制个数为k 求能得到的最长连续相同的字符串的长度 解题思路: 尺取法 维护区间内字符a的个数和字符b的个数 如果numa <= k || numb <= k,说明区间[l,r]可以修改为满足条件的情况,更新答案即可 AC代码: #include <bits/stdc++.h> #define rep(i, n) for (int i = 1; i <= 阅读详情
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
Note

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




已经记不得是第几次在比赛中见到尺取法了……这个方法虽然说不上是什么高深的算法,但真的很有用……

这个题目尺取起来也很简单,两次尺取,一次刷b,一次刷a,看哪次长就行了。以b为例子。num(b)>k为end指针停止的条件,每次计算end指针停止后的(head-end)长度就行。


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
	int i,j,k,m,n;
	scanf("%d%d",&n,&k);
	getchar();
	char s[100005];
	gets(s);
	int l=strlen(s);
	int head=0;
	int end=0;
	int max1=-1;
	int a=0,b=0;
	 int flag=0; 
	while(!flag)
	{
		while(b<=k  && end<l)
		{
			
			if(s[end]=='a')a++;
			 else b++;
			 end++;
		}
		if(end==l)flag=1;
        if(b>k)
		{end--;
		 b--;
   		}
		max1=max(max1,(end-head));
		while(b>=k && head<=end)
		 {
		 	if(s[head]=='a')a--;
		 	 else b--;
		 	head++;
		 }
		
		
	}
	a=0,b=0;
	flag=0;
	 int max2=-1; 
	 head=0;
	 end=0;
	while(!flag)
	{
		while(a<=k  && end<l)
		{
			
			if(s[end]=='a')a++;
			 else b++;end++;
		}
		if(end==l)flag=1;
		if(a>k)
		{end--;
		 a--;
		}
		max2=max(max2,(end-head));
		if(flag)break;
	//	printf("tou:%d wei:%d:\n",head,end,max2);
		while(a>=k && head<=end)
		 {
		 	if(s[head]=='a')a--;
		 	 else b--;
		 	head++;
		 }
	}
	int Max=max(max1,max2);
	printf("%d\n",Max);
	return 0;
 } 

诶,中间的细节总是会出问题,智商还是太低……


vTestStudio保姆教程之从0到1搭建软件环境 学习HIL测试的必经之路,保姆级教学VT从搭建到测试。 阅读详情

相关推荐

莫兰指数stata命令_一文读懂16个sp系列stata空间计量命令及应用

spcompress:Compress Stata-format shapefile压缩数据成stata格式的shapefile2、Looking at data/观测数据2、Looking at data/观测数据grmap:Graph choropleth maps绘制等值区域图spdistance:Calculator for distance between places计算区域之间距离3...

weixin_39719989的博客 823

Codeforces 676C Vasya and String (两点法)

Codeforces 676C Vasya and String (两点法)

Tc的专栏 1879

cocos2dx lua + protobuf

vs 2015 cocos2dx 3.10  lua  protobuf1. 下载 protobuf 源码     https://github.com/bnnswyp/protobuf/tree/master/protobuf-2.5.0    解压后的文件列表如下所示         其中 vsprojects 文件夹为 protobuf 的vs 工程2. 打开 protobuf.sln 编译...

u014486706的博客 2169

【题解】Vasya and String(贪心 + 尺取法)

八中OJ 洛谷 尺取法:顾名思义,像尺子一样取一段,借用挑战书上面的话说,尺取法通常是对数组保存一对下标,即所选取的区间的左右端点,然后根据实际情况不断地推进区间左右端点以得出答案。之所以需要掌握这个技巧,是因为尺取法比直接暴力枚举区间效率高很多,尤其是数据量大的时候,所以尺取法是一种高效的枚举区间的方法,一般用于求取有一定限制的区间个数或最短的区间等等。 贪心选择性质:算法中每一步选择都是...

cqbzlydd的博客 302

DDR4内存PCB设计实战:8层板Fly-by拓扑信号完整性优化

本文深入探讨了DDR4内存条在8层PCB板上采用Fly-by拓扑时的信号完整性优化实战。文章详细解析了层叠规划、阻抗控制、电源分割、等长布线及串扰抑制等关键设计要点,并强调了仿真验证的重要性,为硬件工程师提供了从理论到实践的完整设计指南与自查清单。

weixin_29296241的博客 340

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 denotes beauty of the string as the maximum length of a substring (con...

amazingee的博客 576

从理论到实践:利用FilterSolutions高效设计7阶椭圆低通滤波器

本文详细介绍了如何利用FilterSolutions软件高效设计7阶椭圆低通滤波器。文章从理论入手,对比了巴特沃斯、切比雪夫等经典滤波器特性,阐述了在要求高衰减陡度(如62dB抑制)和严格通带波纹(0.2dB)时,椭圆滤波器是最优选择。通过分步实战教程,指导读者完成从软件参数设置、性能验证到实际电路布局与调试的全过程,助力射频与高速数字信号处理工程师将理论转化为可靠设计。

jupi8的博客 569

C. Vasya and String [思维]

C. Vasya and String 题意: 长为n,由a,b构成的串,可以做k次改变. 问最后最长的相等字串长度是多少. 思路:  k个肯定连续改的. 想了好久.......... #include&lt;bits/stdc++.h&gt; #define PI acos(-1.0) #define pb push_back #define F first #define S seco...

Haipai1998的博客 601

基于STM32设计的万能红外遥控器(学习型)

当前介绍了一种基于STM32微控制器设计的红外遥控器,支持接收解码功能和发送功能,可以将接收的码原样发送出去。 红外遥控器作为智能设备控制方式之一,在一定程度上满足了人们对于快速方便控制的需求。 当前设计的红外遥控器支持学习模式和发送模式。通过矩阵键盘上的数字键选择发送对应的控制码。 一共支持学习10个控制码,对应矩阵键盘上的数字。0~9。

6573

SAP_MM模块通过CC11 创建物料版本

通过本文,我们了解了 SAP 的[SAP_MM模块通过CC11 创建物料版本]的基本概念和应用方法。当物料主数据、BOM(物料清单)、工艺路线等对象需要进行变更控制时,通过创建“版本”或“更改单”来记录、控制和追溯变更。创建完变更主记录(Change Number)后,可在物料主数据维护时引用它,从而创建物料版本化信息。,在其中可以定义物料、BOM、工艺路线等对象的变更版本信息,从而在系统中实现版本化的物料管理。

我小时候很黑的博客 827

CF676C Vasya and String 题解

使用尺取法(双指针法)。1、由于字符种类只有2种,答案一定是全 a 或全 b。2、情况1:全 a快指针循环移动,并统计字符 b 的数量cntb,直到cntb即将超过k。3、情况2:全 b同上。4、答案即为两种情况取到的最大值。

wbw121124的博客 148

【ARM CoreLink 系列 5.3 -- CI-700 SLC memory 系统组件详细介绍】

在 CI-700 系统级缓存(SLC)内存系统中,HN-F 节点负责管理缓存一致性和数据传输。每个 HN-F 节点可以发送三种类型的 Snoop 请求,以确保系统中多核处理器和缓存之间的一致性。CI-700 中的系统级缓存(SLC)内存系统通过 HN-F 节点支持 TrustZone® 技术。以下是 CI-700 支持的 SN 类型以及配置方法的简要介绍。ARM CI-700 系统级缓存(SLC)是一个分布式、主要排他性(mostly exclusive) 的最后一级缓存,集成在 HN-F 节点内。

CodingCos的博客 898

CF676C Vasya and String

思路 类似朴素的尺取法,分别将序列以a,b来算。 以选a来说,我们对每个b赋值1代表需要1的代价去获得1的价值,对a则代价为0,这样问题转化为一道朴素简单的尺取法。然后对b进行同样操作,取两次操作的最大值即是答案。 代码 #include<iostream> #include<algorithm> using namespace std; const int maxn=1e5+5; int a[maxn]; int main() { ios::sync_with_stdio(

m0_51547083的博客 176

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

[CF676C]Vasya and String(尺取法,原题)

题目链接:http://codeforces.com/contest/676/problem/C 原题题解链接:http://www.cnblogs.com/vincentX/p/5405468.html 太菜了,昨晚光想着分块,却没考虑做尺取的时候,对于不需要变化的,那对计数器到k不做贡献。早上恍然大悟,5min改出来了…鶸一个。 1 /* 2 ━━━━━┒ギリギリ♂ eye...

weixin_30381793的博客 141

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

Vasya and String CodeForces - 676C [二分查找 + 尺取法]

题目链接 题目大意: 给定一个只含a,b的字符串,可以将其中有限个数的a转化成b,b转化成a。 问获得的最大的连续的串的长度 Input 4 2 abba Output 4 Input 8 1 aabaabaa Output 5 code 1 尺取法: #include #include #in

米兰的小科比 338

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

CF之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 su

luckyu1的博客 797

cf #345 div2 C(Vasya and String。双端队列)

题目链接:http://codeforces.com/contest/676/problem/C #include #include #include using namespace std; /*************************************************************************************************

Jstyle的博客 668

Codeforces 676 C Vasya and String【二分查找+预处理】

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 o

mengxiang000000 675

C. Vasya and String(cf)前缀和 + 二分答案

原题链接:Problem - 676C - Codeforces 题目大意:给你一串字符串,只包含a和b两个小写字母。子串的长度是n,然后给了你一个数k,这是你能改变字母的最大个数,然后求改变后能获得的最大连续子串长度是多少。 思路: 1.这个题可以用二分来做,二分最大长度,然后每次判断在这个最大长度之下连续子串能不能由改变至多k个字母获得; 2.这里可以用到前缀和做预处理,把a出现的地方看成1,b看成0,那么选定了一个最大子串长度x,就顺序查找每一个这样长度子串中,把所有字母变成a或b能不能在k

qq_52313416的博客 292

#354(Div.2)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 denotes beauty of the string as the maximum length of a substring (con...

li_wen_zhuo的博客 410

双指针+思维 Codeforces Round #354 (Div. 2) C题 Vasya and String

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 denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equ

不知所云的博客 380

CodeForces 676C 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 denotes beauty of the string as the maximum length of a substring (cons

oinei的博客 266

Codeforces Round #354 (Div. 2) C Vasya and String(尺取法二分)

思路:枚举结尾,然后暴力二分就好了,用前缀和预处理一下,那么前缀和之差,就是你需要改变的个数 #include using namespace std; const int maxn = 1e5+7; int n,k; int sum[maxn]; char s[maxn]; int main() { scanf("%d%d",&n,&k); scanf("%s",s+1)

围巾的ACM博客 855

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 denotes beauty of the string as the maximum length of a substring (cons

Twillz的博客 624

Codeforces 676C - Vasya and String 详解(二分前缀和+尺取两种写法)

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 stri

K键盘里的青春K 1440
上一篇: dinic网络流前向星写法
下一篇: hdu 4355 Party All the Time 初识三分法
ctrss
博客等级 码龄11年 1粉丝 35原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值