题解 | 吐泡泡

吐泡泡

https://www.nowcoder.com/practice/f86fa2221c094b3d8d1fc79bae450d96

#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;

int main() {
    int t;
    cin >> t;
    stack<char> a;
    while (t--) {
        string s;
        cin >> s;
        for (int i = 0; i < s.length(); i++) {
            if (a.empty()) {
                a.push(s[i]);
                continue;
            }
            else if (a.top() == s[i] && s[i] == 'o') {
                a.pop();
                if(!a.empty() && a.top()=='O'){
                    a.pop();
                }
                else{
                    a.push('O');
                }
            } 
            else if (a.top() == s[i] && s[i] == 'O') {
                a.pop();
            }
            else{
                a.push(s[i]);
            }

        }
        
        string temp;
        while (!a.empty()) {
            temp.push_back(a.top());
            a.pop();
        }
        reverse(temp.begin(), temp.end());  // 反转后输出
        cout<<temp<<endl;
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务