#吐泡泡# cpp

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

小鱼儿吐泡泡,嘟嘟嘟冒出来。小鱼儿会吐出两种泡泡:大泡泡"O",小泡泡"o"。
两个相邻的小泡泡会融成一个大泡泡,两个相邻的大泡泡会爆掉。
(是的你没看错,小气泡和大气泡不会产生任何变化的,原因我也不知道。)
例如:ooOOoooO经过一段时间以后会变成oO
tpp
输入描述:

数据有多组,处理到文件结束。
每组输入包含一行仅有'O''o'组成的字符串。

输出描述:

  • 每组输出仅包含一行,输出一行字符串代表小鱼儿吐出的泡泡经过融合以后所剩余的泡泡。

示例1

  • 输入
ooOOoooO
  • 输出
oO

说明

  • 自左到右进行合并

备注:

对于100%的数据,
字符串的长度不超过100。


WA:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    stack<char> st;
    string s, str;
    while(cin >> s)
    {
        st.push(s[0]);
        for (int i = 1; i < s.size();i++)
        {
            if(st.empty() || st.top() != s[i])
            {
                st.push(s[i]);
                continue;
            }
            if(!st.empty() && st.top() == s[i] && s[i] == 'o')
            {
                st.pop();
                if(!st.empty() && st.top() == 'O')
                {
                    st.pop();
                }
                else
                {
                    st.push('O');
                }
            }
            else if(!st.empty() && st.top() == s[i] && s[i] == 'O')
            {
                st.pop();
            }
        }
        while(!st.empty())
        {
            str += st.top();
            st.pop();
        }
        for (int i = str.size() - 1; i >= 0;i--)
        {
            cout << str[i];
        }
        cout << endl;
    }
    return 0;
}

WA


AC:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    string s;
    while(cin >> s)
    {
        string str;
        stack<char> st;
        st.push(s[0]);
        for (int i = 1; i < s.size();i++)
        {
            if(st.empty() || st.top() != s[i])
            {
                st.push(s[i]);
                continue;
            }
            if(!st.empty() && st.top() == s[i] && s[i] == 'o')
            {
                st.pop();
                if(!st.empty() && st.top() == 'O')
                {
                    st.pop();
                }
                else
                {
                    st.push('O');
                }
            }
            else if(!st.empty() && st.top() == s[i] && s[i] == 'O')
            {
                st.pop();
            }
        }
        while(!st.empty())
        {
            str += st.top();
            st.pop();
        }
        for (int i = str.size() - 1; i >= 0;i--)
        {
            cout << str[i];
        }
        cout << endl;
    }
    return 0;
}

AC


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值