Codeforces Round #354 (Div. 2) C. Vasya and String
2016-05-26 17:05
375 查看
[b]Vasya and String[/b]
time limit:1 second
memory limit :256 megabytes
Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
The second line contains the string, consisting of letters 'a' and 'b' only.
abba
这个字符串只含有a和b字符
枚举开头,然后暴力二分就好了。
用前缀和预处理一下,那么前缀和之差,就是你需要改变的个数……
time limit:1 second
memory limit :256 megabytes
题目连接
http://www.codeforces.com/contest/676/problem/CDescription
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.Sample Input
4 2abba
Sample Output
4Hint
题意
有一个长度为n的字符串,你可以改变最多k次,问你最长的全是一样字符的串是多长这个字符串只含有a和b字符
题解:
第三次没看出用二分做这类题了,我要好好总结下二分做题的类型与技巧枚举开头,然后暴力二分就好了。
用前缀和预处理一下,那么前缀和之差,就是你需要改变的个数……
代码
#include<bits/stdc++.h> using namespace std; const int maxn=100000+7; char s[maxn]; int a[maxn],b[maxn]; int n,k; bool check(int x) { for(int i=1;i+x-1<=n;i++){ int aa=a[i+x-1]-a[i-1]; int bb=b[i+x-1]-b[i-1]; if(aa<=k||bb<=k) return true; } return false; } int main() { //freopen("in.txt","r",stdin); scanf("%d %d",&n,&k); a[0]=b[0]=0; scanf("%s",s+1); for(int i=1;i<=n;i++){ a[i]=a[i-1]+(s[i]=='a'); b[i]=b[i-1]+(s[i]=='b'); } int l=1,r=n+1; while(l+1<r){ int mid=(l+r)/2; if(check(mid)) l=mid; else r=mid; } printf("%d",l); return 0; }
相关文章推荐
- 处理移动端边框1px问题
- 如何对HashMap按键值排序
- 如何为Tomcat服务器上的多个app配置公共的logback.xml
- 对数据进行分页显示到table中的实现方法
- wonderware配置-Intouch读取Historian数据 8
- 卸载mac系统下openfire
- python 练习 20
- python 练习 19
- Android SurfaceView播放视频
- HDU_1026_Ignatius and the Princess I_BFS(保存路径)
- Objective-C Runtime
- hdu 1074 Doing Homework
- Java 匿名内部类
- Python 还是用eclipse好
- HowTo: Debug Crashed Linux Application Core Files Like A Pro
- jquery ajax 无刷新分页
- 2016SDAU课程练习三1016 Problem P
- Java通过Executors提供四种线程池
- RxJava变换操作符:.concatMap( )与.flatMap( )的比较
- AR7240芯片 EthernetSwitch模块 学习笔记