zstuoj 4246 萌新吃果果

本文介绍了一种使用O(n)时间复杂度进行序列构造的算法实现,通过读取输入字符并根据不同条件更新序列,确保特定条件下序列的正确生成。


点击打开链接


输入的时候直接构造序列,O(n)。




#include<bits/stdc++.h>
#define ll long long
const int maxn=100000+100000;
char team[maxn];
using namespace std;
int main(){
    int T,ans,n,k,i,j,t;
    char c;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&k);
        getchar();ans=0,t=0;
        for(i=0;ans<=n;){
            scanf("%c",&c);ans++;
            if(!i) {i++;continue;}
            else{
                if(c=='a'){
                    team[i++]=c;t++;
                }else if(c=='b'){
                    if(team[i-1]=='a') {
                        team[i++]='0'; t=0;
                    }
                    team[i++]='b';t++;
                }else{
                    int f=0;
                    if(t>=k) f=1;
                    if(f) {
                        team[i++]='0';t=0;
                    }
                    team[i++]='c';t++;
                }
            }
        }
        printf("%d\n",i-1);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值