Codeforces 676C Vasya and String 尺取法
2016-06-06 21:20
141 查看
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
output
input
output
题意:给你一个长度为n字符串,然后通过k次操作(a->b或者b->a).问你能得到最长连续相同的串的长度;
思路:直接尺取就好;
代码:
#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 maxn int(1e5+50)
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
#define PI acos(-1.0)
typedef long long ll;
char s[maxn];
int main()
{
#ifdef CDZSC_June
freopen("t.txt", "r", stdin);
#endif
int n,m,suma,sumb;
while(~scanf("%d%d",&n,&m))
{
suma = sumb = 0;//suma表示a的数量,sumb表示b的数量
scanf("%s",s);
int L =0 ,R = 0;
int max1 = 0;
while(1)
{
while(R < n && min(suma,sumb) <= m){
if(s[R] == 'a') suma++;
if(s[R] == 'b') sumb++;
R++;
}
if(min(suma,sumb) <= m) break;
max1 = max(max1,R- L-1);
while(min(suma,sumb) > m){
if(s[L] == 'a') suma--;
if(s[L] == 'b') sumb--;
L++;
}
}
max1 = max(max1,R- L);
printf("%d\n",max1);
}
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 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
题意:给你一个长度为n字符串,然后通过k次操作(a->b或者b->a).问你能得到最长连续相同的串的长度;
思路:直接尺取就好;
代码:
#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 maxn int(1e5+50)
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
#define PI acos(-1.0)
typedef long long ll;
char s[maxn];
int main()
{
#ifdef CDZSC_June
freopen("t.txt", "r", stdin);
#endif
int n,m,suma,sumb;
while(~scanf("%d%d",&n,&m))
{
suma = sumb = 0;//suma表示a的数量,sumb表示b的数量
scanf("%s",s);
int L =0 ,R = 0;
int max1 = 0;
while(1)
{
while(R < n && min(suma,sumb) <= m){
if(s[R] == 'a') suma++;
if(s[R] == 'b') sumb++;
R++;
}
if(min(suma,sumb) <= m) break;
max1 = max(max1,R- L-1);
while(min(suma,sumb) > m){
if(s[L] == 'a') suma--;
if(s[L] == 'b') sumb--;
L++;
}
}
max1 = max(max1,R- L);
printf("%d\n",max1);
}
return 0;
}
相关文章推荐
- java连接Sql server 2008步骤
- maven之clean、install命令
- C++连接MYSQL数据库
- 内部类与匿名内部类的概念与使用————Java基础知识
- TCP/IP入门(3) --传输层
- Shell 替换+运算符
- iOS Hit-Testing事件响应链
- 总是在起头可是能怎么办呢 Python数据分析
- C++的标准命名空间使用
- spark之DataFrame 通过反射创建
- sort() 排序函数 入门学习
- 面向对象的程序设计-理解对象
- Python 多进程/多线程 学习笔记
- 从头学android_IP拨号器
- 2016"百度之星" - 复赛(Astar Round3)1003 1004
- hiho第七周--完全背包
- POJ 2406 Power Strings
- Android开发笔记之ContentProvider 知识小结和总结
- iOS面试题
- Alpha版总结会议