关闭

codeforces 676C Vasya and String(尺取法)

标签: ACMcodeforcessubstring尺取法
472人阅读 评论(0) 收藏 举报
分类:

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 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?

Input

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.

Output

Print the only integer — the maximum beauty of the string Vasya can achieve by changing no more thank characters.

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".

                


解题思路:尺取法,注意要分为两种情况讨论,都转化为a或者都转化为b。


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
#define MP make_pair
#define PB push_back
#define AA first
#define BB second
#define OP begin()
#define ED end()
#define SZ size()
#define cmin(x,y) x=min(x,y)
#define cmax(x,y) x=max(x,y)
const LL MOD = 1000000007;
const double PI = acos(-1.);
const double eps = 1e-9;
char c[100005];
int A[100005];
int fa(int le,int re){return A[re]-A[le-1];}
int fb(int le,int re){return re-le+1-fa(le,re);}
int main(){
	int i,j,k,l,r;
	int n,m;
	while(~scanf("%d%d",&n,&m)){
		scanf("%s",c+1);
		A[0]=0;
		for(i=1;i<=n;i++)
			A[i]=A[i-1]+(c[i]=='a');//保存前n位有几个a
		int ans=m;//至少可以得到一个长度为m的字串
		l=1;r=1;
		while(l<=n)
        {
            while(r<=n && fa(l,r)<=m)
            {
                cmax(ans,r-l+1);
                r++;
            }
            l++;
        }
        l=1;r=1;
		while(l<=n)
        {
            while(r<=n && fb(l,r)<=m)
            {
                cmax(ans,r-l+1);
                r++;
            }
            l++;
        }
		cout<<ans<<"\n";
	}
	return 0;
}

0
0
查看评论

codeforces 676C Vasya and String (尺取法)

题目:http://codeforces.com/problemset/problem/676/C 代码:#include using namespace std; const int maxn=1e5+5; char a[maxn]; int n,k; int main() { scan...
  • qq_32473657
  • qq_32473657
  • 2016-05-26 21:52
  • 111

Codeforces 676C Vasya and String 尺取法

C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output High schoo...
  • Turne
  • Turne
  • 2016-06-06 21:20
  • 191

codeforces676C. Vasya and String

C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output ...
  • zjw6463
  • zjw6463
  • 2016-05-26 13:01
  • 392

codeforces 676C C. Vasya and String 尺取法

传送门:codeforces 676C C. Vasya and String(二分)题目大意问最多改变k个字母才能使相同字母组成的子串最长;解题思路因为题目中给定的只有a或者b字符,最长的方式无非就两种把a变为b,或者把b变为a。 //就那把b变为a举例子 我们想从左向右一直记录b的的个数,当...
  • woshinannan741
  • woshinannan741
  • 2016-07-20 11:44
  • 217

Codeforces 676C Vasya and String

题目链接:http://codeforces.com/problemset/problem/676/C 题意:给一个只含有ab的字符串,最多修改k个字符使得一段连续字符串只包含一种字符的长度最长。 思路:尺取法,记录当前连续区间字符串中a的个数,b的个数和当前区间左右边界。现在右...
  • chy20142109
  • chy20142109
  • 2016-06-05 17:15
  • 149

Vasya and String CodeForces - 676C [二分查找 + 尺取法]

题目链接 题目大意: 给定一个只含a,b的字符串,可以将其中有限个数的a转化成b,b转化成a。 问获得的最大的连续的串的长度 Input 4 2 abba Output 4 Input 8 1 aabaabaa Output 5 code 1 尺取法: #...
  • khn64
  • khn64
  • 2017-10-21 21:54
  • 66

codeforces 676C Vasya and String

Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output High ...
  • SCAU_BIN
  • SCAU_BIN
  • 2016-05-27 00:43
  • 606

codeforces 676C Vasya and String 前缀数组+二分查找

C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard input output standard outp...
  • EventQueue
  • EventQueue
  • 2016-05-28 10:22
  • 515

codeforces 676C (尺取法)

C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard input output standard outp...
  • morejarphone
  • morejarphone
  • 2016-05-26 10:34
  • 358

Codeforces 676C Vasya and String (两点法)

Codeforces 676C Vasya and String (两点法)
  • Tc_To_Top
  • Tc_To_Top
  • 2016-05-26 14:30
  • 1145
    个人资料
    • 访问:43448次
    • 积分:1933
    • 等级:
    • 排名:千里之外
    • 原创:154篇
    • 转载:0篇
    • 译文:0篇
    • 评论:5条
    文章分类
    最新评论