codeforces676C. Vasya and String

本文介绍了一种算法,旨在通过更改限定次数内的字符来最大化字符串中连续相同字符的最大长度。利用队列记录需要更改的字符位置,实现高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


题意:给你一个字符串,只有a,b,最多k此操作,每次操作可以任意改变一个字符,求最长的连续相同字符长度


思路:直接用队列计入要改的字符的位置, 然后跑一边就可以了,复杂度O(2 * n);


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
const int inf = 2147483647;
const double PI = acos(-1.0);
const double e = 2.718281828459;
const int mod = 1000000007;
typedef long long LL;
#pragma comment(linker, "/STACK:102400000,102400000")
//freopen("in.txt","r",stdin); //输入重定向,输入数据将从in.txt文件中读取
//freopen("out.txt","w",stdout); //输出重定向,输出数据将保存在out.txt文件中cin
char s[100005];
queue<int>sq;
int main()
{
	int n, k, i, j;
	while (cin >> n >> k)
	{
		cin >> s + 1;
		int mmax = 0;
		int last = 1;
		int ans = 0;
		while (!sq.empty())
			sq.pop();
		for (i = 1; i <= n; ++i)
		{
			if (s[i] == 'b')
			{			
				if (sq.size() < k)
				{
					sq.push(i);
					mmax = max(mmax, i - last + 1);
				}
				else 
				{
					if (sq.size() != 0)
					{
						last = sq.front() + 1;
						sq.pop();
						sq.push(i);
					}
					else
						last = i + 1;
				}				
				ans = 1;
			}
			else
				mmax = max(mmax, i - last + 1);
		}
		//cout << ans << endl;
		if (!ans)
			mmax = n;
		while (!sq.empty())
			sq.pop();
		ans = 0;
		last = 1;
		for (i = 1; i <= n; ++i)
		{
			if (s[i] == 'a')
			{
				//mmax = max(mmax, i - 1 - last + 1);
				if (sq.size() < k)
				{
					sq.push(i);
					mmax = max(mmax, i - last + 1);
				}
				else
				{
					if (sq.size() != 0)
					{
						last = sq.front() + 1;
						sq.pop();
						sq.push(i);
					}
					else
						last = i + 1;
				}
				ans = 1;
			}
			else
				mmax = max(mmax, i - last + 1);
		}
		if (!ans)
			mmax = n;
		cout << mmax << endl;

	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值