小鱼儿吐泡泡,嘟嘟嘟冒出来。小鱼儿会吐出两种泡泡:大泡泡"O",小泡泡"o"。
两个相邻的小泡泡会融成一个大泡泡,两个相邻的大泡泡会爆掉。
(是的你没看错,小气泡和大气泡不会产生任何变化的,原因我也不知道。)
例如:ooOOoooO经过一段时间以后会变成oO。
输入
数据有多组,处理到文件结束。
每组输入包含一行仅有'O'与'o'组成的字符串。
输出
每组输出仅包含一行,输出一行字符串代表小鱼儿吐出的泡泡经过融合以后所剩余的泡泡。
样例输入 Copy
ooOOoooO
样例输出 Copy
oO
提示
样例解释:自左到右进行合并
对于100%的数据,字符串的长度不超过100。
#include <bits/stdc++.h>
using namespace std;
const int MX = 100;
char pre[MX + 10];
stack<char> st;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string b;
int cnt = 0;
while (cin >> b)
{
memset(pre, 0, sizeof(pre));
cnt = 0;
st.push(b[0]);
for (int i = 1; i < b.size(); i++)
{
st.push(b[i]);
while (st.size() > 1)
{
char ch = st.top();
st.pop();
char str = st.top();
st.pop();
if (ch == str)
{
if (ch == 'O')
{ // 两个大泡泡
continue;
}
else
{ // 两个小泡泡合并为大泡泡
st.push('O');
}
}
else
{ // 不一样的泡泡没有反应
st.push(str);
st.push(ch);
break;
}
}
}
while (!st.empty())
{
char ch = st.top();
pre[cnt++] = ch;
st.pop();
}
for (int i = cnt - 1; i >= 0; i--)
{
cout << pre[i];
}
cout << '\n';
}
return 0;
}
590





