Codeforces Round #354 (Div. 2)(C)尺取
2016-05-26 10:31
260 查看
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 kcharacters.
Examples
input
output
input
output
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".
题意:你有长度为n的字符串,字符串只有字符a,b组成,现在你可以任意改k个字母,问最长的连续子串有多长?
题解:这里我们可以直接考虑使用尺取法,字母a,b分别跑一遍,找出最大的答案
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
#define N int(1e5+10)
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
typedef long long LL;
#ifdef CDZSC
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
char s
;
int main()
{
#ifdef CDZSC
freopen("i.txt", "r", stdin);
//freopen("o.txt","w",stdout);
int _time_jc = clock();
#endif
//ios::sync_with_stdio(false);
int n,k;
while (~scanf("%d%d%s", &n,&k,s))
{
int ans = 0;
int cost = k;
int L = 0, R=0;
while (L < n&&R<n)
{
while (cost>=0&&R<n)
{
if (s[R] == 'b'&&!cost)break;
if (s[R] == 'b')
{
cost--;
}
ans = max(R - L + 1, ans);
R++;
}
if (s[L] == 'b')cost++;
L++;
}
L = 0, R = 0;
cost = k;
while (L < n&&R<n)
{
while (cost>=0 && R<n)
{
if (s[R] == 'a'&&!cost)break;
if (s[R] == 'a')cost--;
ans = max(R - L + 1, ans);
R++;
}
if (s[L] == 'a')
cost++;
L++;
}
printf("%d\n", ans);
}
#ifdef CDZSC
debug("time: %d\n", int(clock() - _time_jc));
#endif
return 0;
}
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 kcharacters.
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".
题意:你有长度为n的字符串,字符串只有字符a,b组成,现在你可以任意改k个字母,问最长的连续子串有多长?
题解:这里我们可以直接考虑使用尺取法,字母a,b分别跑一遍,找出最大的答案
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
#define N int(1e5+10)
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
typedef long long LL;
#ifdef CDZSC
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
char s
;
int main()
{
#ifdef CDZSC
freopen("i.txt", "r", stdin);
//freopen("o.txt","w",stdout);
int _time_jc = clock();
#endif
//ios::sync_with_stdio(false);
int n,k;
while (~scanf("%d%d%s", &n,&k,s))
{
int ans = 0;
int cost = k;
int L = 0, R=0;
while (L < n&&R<n)
{
while (cost>=0&&R<n)
{
if (s[R] == 'b'&&!cost)break;
if (s[R] == 'b')
{
cost--;
}
ans = max(R - L + 1, ans);
R++;
}
if (s[L] == 'b')cost++;
L++;
}
L = 0, R = 0;
cost = k;
while (L < n&&R<n)
{
while (cost>=0 && R<n)
{
if (s[R] == 'a'&&!cost)break;
if (s[R] == 'a')cost--;
ans = max(R - L + 1, ans);
R++;
}
if (s[L] == 'a')
cost++;
L++;
}
printf("%d\n", ans);
}
#ifdef CDZSC
debug("time: %d\n", int(clock() - _time_jc));
#endif
return 0;
}
相关文章推荐
- 数据结构与算法的分析 —— 渐进复杂度(三个记号)
- Queue LifoQueue and PriorityQueue
- 解决方法:android 6.0(api 23) SDK,不再提供org.apache.http.*(只保留几个类)
- 侧滑
- Verilog基础知识1(FPGA小数运算处理--定点小数)
- Javascript水平提升
- nyoj 762第k个互质数 poj 2773Happy 2006
- sql语句里的if用法实例
- 关于ecplise导入项目之后在tomcat里无法添加的解决方法
- c# 继承 virtual override new
- C#连接Excel的连接字符串
- codeforces 676B (DP 水)
- css二阴影+第三维度z-index
- 使用zsh的十大优点
- Spark的RDD检查点实现分析
- 解决:Hibernate: Encountered a duplicated sql alias [] during auto-discovery of a native-sq
- 在jsp中获取已选中checkbox其他列的值
- Nginx负载均衡
- 网页标题加图片
- 找水王续