Vasya and String(尺取法)

本文介绍了一种算法解决字符串问题,即通过限定次数的字符更改来最大化连续相同字符子串的长度。利用队列实现滑动窗口的方法,分别针对字符'a'和'b'进行优化处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

题意:给你一串字符串和k个修改字符串的机会,让这个字符串获得最长连续相同子串

 

题解:所谓尺取法,顾名思义,就是一把尺子(固定某一条件),不断向右(向左)移动,不断更细我们要的答案。在这里,我们只要从左往右,让修改的字符个数从0慢慢增加到k,中途将字符改成同一个字符(a改成b或者b改成a都行),最后修改字符数固定为k,每次向右移动时,如果字符串需要修改,那就改掉右面的字符,将之前最左边的字符换回来。那么我们可以用一个队列去实现。

如果你看不懂上面也没关系,我再讲具体一些。我们从左边开始,扫描字符串。如果遇到a,那就把a丢进队列,如果遇到b,且此时队列中b的个数不超过k的话,就把b丢进去。如果b的个数等于k,且又遇到一个b,那就记录下此时队列长度,这个时候队列里k个b可以当作a,所以可以记录队列元素个数,更新答案最大值。之后将队列前面元素弹出,直到弹出一个b,再将新的b压进队列……一直扫描直到字符串尾。

接下来将a和b互换,重复上面步骤,更新最大值。最后输出最大值。

代码如下

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
char s[100100];
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    scanf("%s",s);
    queue <char> Q;
    int maxn=0;
    int len=0;
    for (int i=0; i<n; i++)
    {
        if (s[i]=='a')
            Q.push(s[i]);
        else if (len<k)
        {
            len++;
            Q.push(s[i]);
        }
        else
        {
            maxn=max(maxn,(int)Q.size());
            while (!Q.empty()&&Q.front()=='a')
                Q.pop();
            if (!Q.empty())
            {
                Q.pop();
                Q.push(s[i]);
            }
        }
    }
    maxn=max(maxn,(int)Q.size());
    while (!Q.empty()) Q.pop();
    len=0;
    for (int i=0; i<n; i++)
    {
        if (s[i]=='b')
            Q.push(s[i]);
        else if (len<k)
        {
            len++;
            Q.push(s[i]);
        }
        else
        {
            maxn=max(maxn,(int)Q.size());
            while (!Q.empty()&&Q.front()=='b')
                Q.pop();
            if (!Q.empty())
            {
                Q.pop();
                Q.push(s[i]);
            }
        }
    }
    maxn=max(maxn,(int)Q.size());
    printf("%d\n",maxn);
    return 0;
}

 

转载于:https://www.cnblogs.com/scaugsh/p/5534204.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值