题解 | 吐泡泡

吐泡泡

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

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        string s;
        cin>>s;
        stack<char> sta;
        for(auto c:s)
        {
            if(sta.empty()) sta.push(c);
            else {
                if(!sta.empty()&&sta.top()=='o'&&c=='o')
                {
                    sta.pop();
                    if(!sta.empty()&&sta.top()=='O') sta.pop();
                    else sta.push('O');
                    
                }
                else if(!sta.empty()&&sta.top()=='O'&&c=='O')
                {
                    sta.pop();
                }
                else sta.push(c);
            }
        }
        string str;
        while(!sta.empty())
        {
            str+=sta.top();
            sta.pop();
        }
        for(int i=(int)str.length()-1;i>=0;i--)
        {
            cout<<str[i];
        }
        cout<<endl;
    }
    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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