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

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


思科 MIB 库 思科 MIB 阅读详情

相关推荐

Vscode远程免密连接Linux虚拟机,并配置C/C++运行环境

目前想要深入学习在Linux,也为了方便编写C/C++程序。Linux的环境里一般自带有c++的编译环境,如果没有安装gcc和g++等编译器也比较便利。但是在虚拟机里用vim编写C++程序又不得劲,因此打算使用vscode远程连接Linux虚拟机进行学习。为了防止自己以后还要再配置,重复踩坑,因此写下这篇博客用做记录。环境:windows11、VMware17、rocky linux9.1 ( 开源的CentOS9 )

m0_56779872的博客 8676

Codeforces 676C Vasya and String (两点法)

Codeforces 676C Vasya and String (两点法)

Tc的专栏 1878

TP4056锂电池充电电路设计原理与工程实践

锂离子电池充电管理是嵌入式电源系统的核心基础技术,其本质是通过恒流恒压(CC/CV)控制策略,匹配电池电化学特性以实现安全、高效、长寿命充放电。TP4056作为典型线性充电IC,以精简外围实现预充、恒流、恒压及自动截止全周期管理,但受限于线性拓扑的热耗散特性,必须协同散热设计、电阻精度选型与PCB布局进行系统级优化。该方案广泛应用于STM32智能小车、便携传感器节点等3.7V单节锂电供电场景,兼顾成本、可靠性与工程可实施性,是入门嵌入式硬件电源设计的关键实践范例。

weixin_36064575的博客 219

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

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

cqbzlydd的博客 302

STM32实战指南:TFTLCD液晶屏驱动与ILI9341应用解析

本文详细解析了STM32驱动TFTLCD液晶屏的实战技巧,重点介绍了ILI9341控制器的硬件连接、寄存器操作及性能优化方法。通过8080并行接口时序分析、GRAM操作优化和DMA传输应用,帮助开发者高效实现液晶屏驱动,适用于工业控制、智能家居等场景。

weixin_26873711的博客 232

Apollo Planning2.0决策规划算法代码详细解析 (1):环境搭建

1、官方提供连接:git clone https://github.com/ApolloAuto/application-pnc.git。此外,也更新了dreamview对pnc的支持,使得调试更加方便。apollo开源团队近期更新了planning版本,对代码进行了一定程度上的重构。(2)进入docker,并编译整个工程:./apollo.sh build_dbg。下载项目环境后使用aem进行环境配置;2、有一定基础建议直接拉取最新代码,自己手动进行编译。(1)环境安装: ./dev_start.sh。

自动驾驶Player的博客 1840

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

JNI的本质解析:Android Framework视角下的Java-Native衔接机制

本文基于Android 14源码深入分析JNI机制,从Android Framework视角解析Java-Native衔接原理。主要内容包括:JNI的双轨制注册机制(静态与动态注册),核心数据结构(JNIEnv与JavaVM)的实现原理,以及完整的调用链路剖析。文章通过MessageQueue的JNI实现案例,展示Android Framework如何利用JNI实现Java层与Native层的高效交互,并提供了性能优化建议。分析表明,JNI作为Android系统架构的关键桥梁,其设计充分考虑了性能、安全性

保持专注,保持热爱 1209

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

秃鹰搜索优化算法(BES)详解:从自然捕食到全局优化

秃鹰搜索优化算法(Bald Eagle Search Optimization Algorithm,BES)是由马来西亚学者Alsattar等人于2020年提出的一种新型元启发式优化算法。该算法灵感来源于秃鹰在自然界中高超的捕食行为,特别是其独特的狩猎策略和卓越的视觉搜索能力,模拟了秃鹰捕食鲑鱼等猎物的完整过程。

qq_42212808的博客 1350

HarmonyOS 鸿蒙开发DevEco Studio OpenHarmony:安全测试

通过对HarmonyOS应用生命周期建模和应用攻击面建模,采用静态数据流分析技术,提高漏洞发现的准确率,同时覆盖20余种攻击面,65+漏洞测试项,帮助开发者提前发现和识别漏洞隐患。在检测报告中,会针对每一项漏洞风险项给出明确的修复建议,可以帮助开发者快速修复漏洞。

斌擎科技 1181

CF676C Vasya and String 题解

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

wbw121124的博客 145

LangGraph 实战教程:搭建一个智能办公多Agent协作平台

只存储必要的信息:不要把所有的原始数据都存储在状态中,比如销售数据可以存储在数据库或向量数据库中,状态中只存储数据的 ID 或摘要。使用类型提示:避免类型错误,提高代码的可维护性。支持增量更新:LangGraph 默认支持状态的增量更新(通过reduce函数),比如messages列表可以通过追加新消息的方式更新,而不是每次都重新创建整个列表。Normal Edge 的定义非常简单,只需要在StateGraph对象(稍后会详细讲解)上调用add_edge# 创建一个 StateGraph 对象。

SuperAGI2025 221

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

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

[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

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

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

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

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、付费专栏及课程。

余额充值