题解 | 吐泡泡

吐泡泡

https://www.nowcoder.com/practice/f86fa2221c094b3d8d1fc79bae450d96?tpId=383&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj

#include <iostream>
#include <stack>

using namespace std;



void output(string str)
{
    stack<char> ch;
    for(int i =0;i<str.size();i++)
    {
        if((str[i]=='o')&&(ch.empty()==true))
        {
            ch.push(str[i]);
        }
        else if((str[i]=='o')&&(ch.empty()!=true))
        {
            if(ch.top()=='o')
            {
                ch.pop();
                if(ch.empty()!=true)
                {
                    if(ch.top()=='O')
                        ch.pop();
                    else
                        ch.push('O');
                }
                else
                {
                    ch.push('O');
                }
            }
            else
            {
                ch.push(str[i]);
            }
        }
        else if((str[i]=='O')&&(ch.empty()==true))
        {
            ch.push(str[i]);
        }
        else if((str[i]=='O')&&(ch.empty()!=true))
        {
            if(ch.top()=='O')
            {
                ch.pop();
            }
            else
            {
                ch.push(str[i]);
            }
        }
        else
        {

        }
    }

    stack<char> res;

    while(ch.empty()!=true)
    {
        res.push(ch.top());
        ch.pop();
    }
    while(res.empty()!=true)
    {
        cout<<res.top();
        res.pop();
    }
    cout<<endl;

}

int main() {

    string str;
    int loop;

    cin>>loop;

    while(loop--)
    {
        cin>>str;
        output(str);
    }
    
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

码农索隆:以下是我以我微薄的认知提供的建议: 1.考个教师资格证,去当体育考试。 2.去健身房当健身教练(因为在我印象里面体育生身材都不错)。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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