链接: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;
}