[CodeForces-676C] 尺取法
来源:互联网 发布:网店美工工作职责 编辑:程序博客网 时间:2024/04/30 06:51
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 asubstring (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?
The first line of the input contains two integers n andk (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.
Print the only integer — the maximum beauty of the string Vasya can achieve by changing no more thank characters.
4 2abba
4
8 1aabaabaa
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 <bits/stdc++.h>using namespace std;const int MAXN = 100000+5;int n, k;char s[MAXN];int cal(char ch) {int l = 0, r = 0, ans = 0;int sum = 0;//当前已更改的字符个数while (l < n && r < n) {while ((sum < k || s[r] == ch) && r < n) {if (s[r] != ch) {sum++;}r++;}ans = max(ans, r-l);while (l <= r && s[l] == ch) {l++;}l++;sum--;}return ans;}int main(){while (~scanf("%d %d", &n, &k)) {scanf("%s", s);printf("%d\n", max(cal('a'), cal('b')));}return 0;}
- codeforces 676C (尺取法)
- [CodeForces-676C] 尺取法
- codeforces 701 c 尺取法
- codeforces 676C C. Vasya and String 尺取法
- codeforces 676C Vasya and String (尺取法)
- codeforces 676C Vasya and String(尺取法)
- Codeforces 676C Vasya and String 尺取法
- (尺取法)Codeforces 676C - Vasya and String
- codeforces 660C (尺取法 水~)
- codeforces 701C(尺取法)
- Educational Codeforces Round 6(C)尺取法+贪心
- Codeforces 660C-Hard Process【尺取法经典练习】
- 【CodeForces】660C - Hard Process(二分,尺取法)
- codeforces 701C They Are Everywhere(尺取法)
- Codeforces Round #364 (Div. 2) C 尺取法
- codeforces--701C They Are Everywhere(尺取法)
- codeforces problem 701 C They Are Everywhere尺取法
- 【CodeForces】701C - They Are Everywhere(尺取法)
- 将博客搬至CSDN
- 写了一个小工程——技术思考
- 1044. 火星数字(20)
- 读书笔记:Geometric and Fourier methods for nonliner wave equations
- 树莓派运行opencv和caffe 配置Qt creator
- [CodeForces-676C] 尺取法
- 1045. 快速排序(25)
- 405. Convert a Number to Hexadecimal
- getopt函数
- 2016搜索提高1001
- 1046. 划拳(15)
- IO流之显示指定类型的文件
- IPython常用用法
- java.sql.Date 和java.util.Date并不相同