题解:P15538 [CCC 2026 J3] Creative Candy Consumption

· · 题解

P15538 [CCC 2026 J3] Creative Candy Consumption 题解

::::info[化简题目]

模拟过程

  1. 我们可以用两个队列来存储糖果序列

  2. 接着循环比较队首:

    • 同色:两队队首都弹出,计数器各 +1;
    • 异色:败者队首弹出,胜者计数器 +1。
  3. 处理剩余糖果
    ::::success[代码实现]
    ```cpp
    #include <bits/stdc++.h>
    using namespace std;

define int long long

signed main(){ ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);

string s1, s2;
cin >> s1 >> s2;

queue<char> ngoc, minh;

for(int i = 0; i < s1.length(); i++) ngoc.push(s1[i]);
for(int i = 0; i < s2.length(); i++) minh.push(s2[i]);

int sum1 = 0, sum2 = 0;
while(!ngoc.empty() && !minh.empty()){
    char n = ngoc.front();
    char m = minh.front();

    if(n == m){
        sum1++;
        sum2++;
        ngoc.pop();
        minh.pop();
    }

    else if(n == 'R' && m == 'G'){
        minh.pop();
        sum1++;
    }

    else if(n == 'G' && m == 'R'){
        ngoc.pop();
        sum2++;
    }

    else if(n == 'G' && m == 'B'){
        minh.pop();
        sum1++;
    }

    else if(n == 'B' && m == 'G'){
        ngoc.pop();
        sum2++;
    }

    else if(n == 'B' && m == 'R'){
        minh.pop();
        sum1++;
    }

    else if(n == 'R' && m == 'B'){
        ngoc.pop();
        sum2++;
    }

    /*
    当然你也可以这样写会更简洁,我上面这样写是想清晰一点 
    else if((n == 'R' && m == 'G') || (n == 'G' && m == 'B') || (n == 'B' && m == 'R')){
        minh.pop();
        sum1++;
    }

    else{
        ngoc.pop();
        sum2++;
    }
    */
}

while(!ngoc.empty()){
    sum1++;
    ngoc.pop();
}
while(!minh.empty()){
    sum2++;
    minh.pop();
}

cout << sum1 << endl << sum2;

return 0;

}



::::
::::warning[恳求]
求管理员审核通过,求各位点赞评论加关注再走吧qwq
::::