Codeforces Round #354 (Div. 2)676C. Vasya and String

本文详细介绍了如何使用C++编程语言解决Codeforces竞赛中关于魅力值最大化的数学问题。通过枚举和区间操作,实现了一种高效的算法来求解目标值。代码简洁易懂,适合初学者学习。

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

题目链接:http://codeforces.com/contest/676/problem/C
题意:给出一个只有a,b的字符串,定义这个字符串的魅力值为“最长的连续的任意一种字符的串的长度” 你现在可以将k个字符转化 请问最大的魅力值
思路:
就枚举每个能用完k的区间就行了…
先枚举右端点,然后找到最远的用掉<=k个字符的区间

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define lowbit(x) (x&(-x))
typedef long long LL;
const int maxn = 100005;
const int inf=(1<<28)-1;
char str[maxn];
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    scanf("%s",str);
    int La=0,Lb=0,tmp_a=0,tmp_b=0,ans=0;
    for(int i=0;i<n;++i)
    {
        if(str[i]=='a') tmp_b++;
        else tmp_a++;
        while(tmp_a>k)
        {
            if(str[La]=='b') tmp_a--;
            La++;
        }
        ans=max(ans,i-La+1);
        while(tmp_b>k)
        {
            if(str[Lb]=='a') tmp_b--;
            Lb++;
        }
        ans=max(ans,i-Lb+1);
    }
    printf("%d\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值