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;
 } 

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


保姆级教程:在Davinci Configurator里配置BswM的ECU状态机(附状态切换流程图) 本文提供了一份详细的AUTOSAR BswM模块配置指南,通过Davinci Configurator工具实现ECU状态机的构建与管理。内容涵盖从模式请求端口创建到状态切换规则定义的全流程,特别针对Ecu State Handling进行了深入解析,并附有状态切换流程图,帮助汽车电子工程师高效完成ECU状态管理配置。 阅读详情

相关推荐

Opencv计算相机响应函数(Camera Response Function)与Radiance

本文主要用于记录自己在学习CRF过程中遇到的一些问题。 CRF模型 I=f(V(t⋅L))I=f(V( t\cdot L))I=f(V(t⋅L)) 其中III为相机灰度值Intensity,LLL为环境中irradiance,ttt为曝光时间,VVV为相机镜头参数,fff为相机响应函数CRF。 有些论文中认为相机镜头参数是线性的不考虑,但有些考虑了衰减即靠近图像边缘亮度会暗一些,但没有看到二者同时计算的,如果同时计算两个非线性的函数,我觉得可能会有不确定性,在这里还请教一下了解的朋友。 控制

larrydong的博客 6000

Codeforces 676C Vasya and String (两点法)

Codeforces 676C Vasya and String (两点法)

Tc的专栏 1878

暗月项目七详解

本文围绕“暗月靶场项目七”展开,记录了一次从外网 Web 入口逐步深入内网并最终获取域控目标的完整实训过程。

Re_K123的博客 416

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

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

cqbzlydd的博客 302

宽带波束形成

介绍了宽带波束形成器的时域滤波结构,以及波束响应的计算公式,宽带波束形成器的波束响应可表示为权重向量和方向向量的积

... 2490

基于Ansys Fluent 的颗粒分离/过滤解决方案

过滤是指通过特殊装置将颗粒移除,将流体提纯净化的过程。过滤的方式很多,应用的物系也很广泛,固-液、固-气、大颗粒-小颗粒等。

sh_djoin的博客 1852

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的博客 575

CAPL 中的变量类型详解与应用

CAPL 中的变量类型详解与应用

qq_42268594的博客 1448

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的博客 600

winform自定义控件(gdi+)(1)———设置透明背景色

本文详细讲解了如何在winform中设置控件的背景色为透明色

qq_34059233的博客 3686

西门子博图v16安装密钥提示报错:CryptAcquireContext MS_DEF_PROV Error of containger opening...如何解决?

🏆本文收录于 《全栈 Bug 调优(实战版)》 专栏。专栏聚焦真实项目中的各类疑难 Bug,从成因剖析 → 排查路径 → 解决方案 → 预防优化全链路拆解,形成一套可复用、可沉淀的实战知识体系。无论你是初入职场的开发者,还是负责复杂项目的资深工程师,都可以在这里构建一套属于自己的「问题诊断与性能调优」方法论,助你稳步进阶、放大技术价值。

**My Coding Family** 3300

CF676C Vasya and String 题解

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

wbw121124的博客 145

MATLAB基础应用精讲-【强化学习】Actor - Critic 算法(附python代码实现)

这种架构结合了策略梯度方法(Policy - Gradient Methods)和价值函数估计方法(Value Function Estimation Methods)的优点。在强化学习中,智能体(Agent)需要通过与环境(Environment)的交互来学习最优的行为策略,以获得最大的累积奖励(Cumulative Reward)。

qq_36130719的博客 363

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的博客 175

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

[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

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

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

米兰的小科比 336

精雕软件(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的博客 796

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 674

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的博客 291

#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的博客 265

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的博客 622
上一篇: 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、付费专栏及课程。

余额充值