题解 | 吐泡泡

吐泡泡

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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

    int T;
    cin >> T;

    for (int i = 0; i < T; i++) // 多组数据
    {
        // 输入数据
        string str;
        cin >> str;

        // 初始化栈
        stack<char> st;

        // 循环读入每一项然后根据规则合并
        for (int j = 0; str[j] != '\0'; j++)
        {
            // 如果是空栈,直接push进去
            if (empty(st) == true)
            {
                st.push(str[j]);
            }
            else // 不是空栈,准备处理
            {
                // 循环处理
                char ch = str[j];
                if (ch != st.top()) // 如果和栈顶不相同,不用管直接push
                {
                    st.push(ch);
                }
                else // 有相同的,开始进行操作
                {
                    char ch = str[j]; // 最终需要push进栈的字符
                    int flag = 0;
                    while (ch == st.top()) // 相同时重复执行
                    {
                        if (ch == 'o') // 合并
                        {
                            st.pop();
                            ch = 'O';
                        }
                        else // 爆炸,直接break,不需要push
                        {
                            st.pop();
                            flag = 1;
                            break;
                        }
                        // 检查是否空栈
                        if (empty(st) == true)
                        {
                            break;
                        }
                    }

                    if (!flag) // 没爆炸
                    {
                        st.push(ch);
                    }
                }
            }
        }

        // 输出
        stack<char> ans;
        while (!st.empty())
        {
            ans.push(st.top());
            st.pop();
        }
        while (!ans.empty())
        {
            cout << ans.top();
            ans.pop();
        }
        cout << '\n';
    }
}

全部评论

相关推荐

在喝茶的杨桃很郁闷:我简单喵两句: 1.如果不是实在没东西写不要写熟悉async await这些语法层面的东西 2.也不要写熟悉HTTP,因为http内容很多,稍微深挖一点你不会的话会让人有一种“原来你简历上面的东西都没有完全掌握”的感觉,容易给自己挖坑 3.自我评价可以删了 4.我在复习明天的面试,先mark,后面再回来继续建议
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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