codeforces676C. Vasya and String

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zjw6463/article/details/51506317
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;

	}
}


猜你喜欢

转载自blog.csdn.net/zjw6463/article/details/51506317