牛客网 15029 (栈)

链接:https://ac.nowcoder.com/acm/problem/15029
来源:牛客网

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

输入:
ooOOoooO

输出:
oO

#include <iostream>
#include <stack> 
#include <string> 
#include <vector>
using namespace std;
/*
堆栈 
*/
stack<char> s;
int main(){
	string str;
	while( cin>>str ){
		int len = str.length();
		s.push(str[0]);
		for(int i=1;i<=len;i++){
			// 取栈顶
			if(str[i] == 'o'){
				if(!s.empty()){
					char ch = s.top();
					if(ch == 'o'){
						s.pop();
						if(!s.empty()){
							s.pop();
						}else{
							s.push('O');
						}
					}else{
						s.push('o');
					}	
				}else{
					s.push('o');
				}
			}
			if(str[i] == 'O'){
				if(s.empty())	s.push('O');
				else{
					char c = s.top();
					if(c == 'O'){
						s.pop();					
					}else{
						s.push('O');
					}		
				}
			}
		}
		char pre[10009];
		int a=0;
 		while (!s.empty())
        {
            char ch=s.top();
            s.pop();
            pre[a++]=ch;
        }
        for (int i=a-1;i>=0;i--) cout<<pre[i];
        cout << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值