Codeforces Round #354 (Div. 2) C. Vasya and String (尺取法)

本文介绍了一道编程题目,该题目要求在只包含'a'和'b'的字符串中找到最长的连续子串,允许更改不超过k个字符。通过使用尺取法,该文提供了一个C++实现方案,能够有效地解决这个问题。

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 k 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”.

题意:一个只有ab的字符串,可以改变最多k个字符,求最长的连续子串,即字符相同

思路:利用尺取的方法,截取在k范围内的不同字符,扫两遍即可。

ac代码:

/* ***********************************************
Author       : AnICoo1
Created Time : 2016-08-22-14.17 Monday
File Name    : D:\MyCode\2016-8月\2016-8-22.cpp
LANGUAGE     : C++
Copyright  2016 clh All Rights Reserved
************************************************ */
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define LL long long
#define ll __int64
#define mem(x,y) memset(x,(y),sizeof(x))
#define PI acos(-1)
#define gn (sqrt(5.0)+1)/2
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
const ll INF=1e9+10;
const int MAXN=1e6+10;
const int MOD=1e9+7;
//head
char str[MAXN];
int main()
{
    int n,k;cin>>n>>k;
    scanf("%s",str);
    int l=0,r=0,ans=0,cnt=0;
    for(;r<n;)
    {
        while(r<n&&cnt<=k)
        {
            if(str[r]=='b')
                cnt++;
            r++;
        }
        if(cnt>k) cnt--,r--;
        ans=max(ans,r-l);
        while(str[l]=='a'&&l<=r)
            l++;
        l++;
        cnt--;
    }
    l=0,r=0,cnt=0;
    for(;r<n;)
    {
        while(r<n&&cnt<=k)
        {
            if(str[r]=='a')
                cnt++;
            r++;
        }
        if(cnt>k) cnt--,r--;
        ans=max(ans,r-l);
        while(str[l]=='b'&&l<=r)
            l++;
        l++;
        cnt--;
    }
    printf("%d\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值