T562362 game 题解

· · 题解

(手工栈好题!!!)

题目大意

给一段字符串,如:acccabbba.连续3个就可以消除, 削完输出GOOD,否则输出剩余字母。

做法

手工栈闪亮出场!只要top>=2,就判断一下前三个是否相等,相等就top-=3,最后判断top是否=-1,是的话cout<<"GOOD"; ,不是就遍历一遍,一个一个输出

AC代码

#include <bits/stdc++.h>
using namespace std;
char stk[100005];
int top = -1;    
int main(){
    string s;
    cin >> s;
    for (int i = 0; i < s.size(); i++){
        char ch = s[i];
        top++;
        stk[top] = ch;
        while (top >= 2){
            if (stk[top] == stk[top-1] && stk[top-1] == stk[top-2]){
                top -= 3; 
            }
            else{
                break;
            }
        }
    }
    if (top == -1){
        cout << "GOOD";
    }
    else{
        for (int i = 0; i <= top; i++){
            cout << stk[i];
        }
    }
    return 0;
}