/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 332.0 KiB
#2 Wrong Answer 2ms 332.0 KiB
#3 Wrong Answer 2ms 332.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int MODD = 998244353;

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//
//template <typename num_t>
//using ordered_set = tree<num_t, null_type, less<num_t>, rb_tree_tag, tree_order_statistics_node_update>;



void solve(){
    int n,k;
    cin>>n>>k;

    string s;
    cin>>s;
    s=' '+s;


    for(int i=1;i<=n-2;){
        if(s[i]=='a' and s[i+1]=='b' and s[i+2]!='c'){
            if(k){
                k--;
                s[i+2]='c';
                i+=3;
            }
            else break;
        }
        else if(s[i]=='a' and s[i+2]=='c' and s[i+1]!='b'){
            if(k){
                k--;
                s[i+1]='b';
                i+=3;
            }
            else{
                break;
            }
        }
        else if(s[i]!='a' and s[i+1]=='b' and s[i+2]=='c'){
            if(k){
                k--;
                s[i]='a';
                i+=3;
            }
            else{
               break;
            }
        }
        else i++;
    }

    for(int i=1;i<=n-2;){
         if(s[i]=='a' and s[i+1]!='b' and s[i+2]!='c'){
            if(k>=2){
                k-=2;
                s[i+2]='c';
                s[i+1]='b';
                i+=3;
            }
            else break;
        }
        else if(s[i]!='a' and s[i+2]!='c' and s[i+1]=='b'){
            if(k>=2){
                k-=2;
                s[i]='a';
                s[i+2]='c';
                i+=3;
            }
            else{
                break;
            }
        }
        else if(s[i]!='a' and s[i+1]!='b' and s[i+2]=='c'){
            if(k>=2){
                k-=2;
                s[i]='a';
                s[i+1]='b';
                i+=3;
            }
            else{
                break;
            }
        }
        else i++;
    }

    for(int i=1;i<=n-2;){
        if((s[i]!='a' and s[i+1]!='b' and s[i+2]!='c')){
            if(i>1 and s[i]=='b' and s[i+1]=='c' and s[i-1]=='a'){
                i+=2;
                continue;
            }
            if(i>2 and s[i]=='c' and s[i-1]=='b' and s[i-2]=='a'){
                i++;
                continue;
            }
            if(k>=3){
                k-=3;
                s[i]='a';
                s[i+1]='b';
                s[i+2]='c';
                i+=3;
            }
            else{
                break;
            }
        }
        else{
            i++;
        }
    }

    int cnt=0;
    for(int i=1;i<=n-2;){
        if(s[i]=='a' and s[i+1]=='b' and s[i+2]=='c'){
            i+=3;
            cnt++;
        }
        else i++;
    }

    cout<<cnt<<'\n';
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);


    int t = 1;
    cin >> t;
    //int T = 0;
    while (t--)
    {
        //T++;
        //cout<<"Case "<<T<<": ";
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1100 Substring ABC
Contest
Brain Booster #6
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-03 16:42:58
Judged At
2024-11-11 02:47:45
Judged By
Score
2
Total Time
2ms
Peak Memory
332.0 KiB