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

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


### 关于 Vasya 和多重集的编程竞赛算法目解决方案 #### 目描述 给定一个多重集合 `s`,目标是将其分割成两个新的多重集合 `a` 和 `b` (其中一个可以为空),使得这两个新集合中的“好数”的数量相等。“好数”定义为在一个特定多集中恰好只出现一次的数字。 为了实现这一目标,需要考虑如何有效地统计并分配这些元素到不同的子集中去[^1]。 #### 解决思路 一种有效的解决方法是从输入数据的特点出发思考。如果某个数值在整个原始集合中出现了偶数次,则该值可以在不影响最终结果的情况下被平均分入两个子集中;而对于那些仅出现奇数次数的情况,则必须小心处理以确保能够达成平衡条件——即让尽可能多的不同类型的单例项分别进入各自的目标组内[^2]。 具体来说: - 对于任何频率大于等于两次(无论是奇还是偶)的数据点而言,总是能通过适当划分来满足上述要求; - 当遇到频度为一的情形时,就需要额外注意了:因为这直接影响着能否成功创建具有相同数目唯一成员的新分区。 因此,在实际编码过程中应该优先处理那些重复率较高的项目,并记录下所有独一无二实例的位置以便后续操作使用。 #### Python 实现代码示例 下面是一个基于此逻辑编写的Python函数,用于求解这个问: ```python from collections import Counter def can_split_equally(s): count = Counter(s) # 统计各元素出现次数 singletons = sum(1 for v in count.values() if v == 1) return singletons % 2 == 0 # 测试用例 test_cases = [ [1, 2, 2, 3], # True 可以分成 {1} 和 {2, 2, 3} [1, 2, 3, 4, 5], # False 单独存在的数字有五个无法平分 ] for case in test_cases: print(f"Input: {case}, Can Split Equally? :{can_split_equally(case)}") ``` 这个程序首先利用 `collections.Counter` 来计算每个整数在列表里边出现过的总次数。接着它会遍历所有的键值对,累积起所有只出现过一次(也就是所谓的 “好数” 或者说是单一实例)的数量。最后一步就是判断这样的特殊案例是不是构成了一个偶数序列长度 —— 如果是的话就意味着存在至少一组可行解;反之则不存在这样的一分为二方式。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值