What is the Codeforces Round #XXX problem set like?
- 内容介绍
- 文章标签
- 相关推荐
本文共计642个文字,预计阅读时间需要3分钟。
高中学生瓦西里收到了一个长度为n的字符串作为生日礼物。这个字符串只包含字母a和b。瓦西里认为字符串的美丽是包含连续子序列的最大长度,其中子序列只包含a或b。
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
inputCopy
4 2
abba
outputCopy
4
inputCopy
8 1
aabaabaa
outputCopy
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”.
双端队列的巧妙应用:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 500005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-7
#define pll pair<ll,ll>
ll quickpow(ll a, ll b) {
ll ans = 1;
a = a % mod;
while (b > 0) {
if (b % 2)ans = ans * a;
b = b / 2;
a = a * a;
}
return ans;
}
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a%b);
}
int main()
{
ios::sync_with_stdio(false);
string s;
int k;
int n; cin >> n;
cin >> k;
cin >> s;
int start = 0;
int ans = 0;
deque<int> de;
for (int i = 0; i < (s.size()); i++) {
if (s[i] == 'b')
de.push_back(i);
if ((de.size()) > k) {
start = de.front() + 1;
de.pop_front();
}
ans = max(ans, i - start + 1);
//cout << ans << endl;
}
de.clear();
start = 0;
for (int i = 0; i < (s.size()); i++) {
if (s[i] == 'a')
de.push_back(i);
if ((de.size()) > k) {
start = de.front() + 1;
de.pop_front();
}
ans = max(ans, i - start + 1);
}
cout << ans << endl;
}
本文共计642个文字,预计阅读时间需要3分钟。
高中学生瓦西里收到了一个长度为n的字符串作为生日礼物。这个字符串只包含字母a和b。瓦西里认为字符串的美丽是包含连续子序列的最大长度,其中子序列只包含a或b。
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
inputCopy
4 2
abba
outputCopy
4
inputCopy
8 1
aabaabaa
outputCopy
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”.
双端队列的巧妙应用:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 500005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-7
#define pll pair<ll,ll>
ll quickpow(ll a, ll b) {
ll ans = 1;
a = a % mod;
while (b > 0) {
if (b % 2)ans = ans * a;
b = b / 2;
a = a * a;
}
return ans;
}
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a%b);
}
int main()
{
ios::sync_with_stdio(false);
string s;
int k;
int n; cin >> n;
cin >> k;
cin >> s;
int start = 0;
int ans = 0;
deque<int> de;
for (int i = 0; i < (s.size()); i++) {
if (s[i] == 'b')
de.push_back(i);
if ((de.size()) > k) {
start = de.front() + 1;
de.pop_front();
}
ans = max(ans, i - start + 1);
//cout << ans << endl;
}
de.clear();
start = 0;
for (int i = 0; i < (s.size()); i++) {
if (s[i] == 'a')
de.push_back(i);
if ((de.size()) > k) {
start = de.front() + 1;
de.pop_front();
}
ans = max(ans, i - start + 1);
}
cout << ans << endl;
}

