NC15029 吐泡泡 栈 暴力模拟
链接:https://ac.nowcoder.com/acm/problem/15029
来源:牛客网
题目描述
小鱼儿吐泡泡,嘟嘟嘟冒出来。小鱼儿会吐出两种泡泡:大泡泡"O",小泡泡"o"。
两个相邻的小泡泡会融成一个大泡泡,两个相邻的大泡泡会爆掉。
(是的你没看错,小气泡和大气泡不会产生任何变化的,原因我也不知道。)
例如:ooOOoooO经过一段时间以后会变成oO。
输入描述:
数据有多组,处理到文件结束。
每组输入包含一行仅有’O’与’o’组成的字符串。
输出描述:
每组输出仅包含一行,输出一行字符串代表小鱼儿吐出的泡泡经过融合以后所剩余的泡泡。
示例1
输入
复制
ooOOoooO
输出
复制
oO
说明
自左到右进行合并
备注:
对于100%的数据,
字符串的长度不超过100。
很容易想到栈
多组数据 !!! !!!
扫描整个字符串 s t r str str
- 当栈空,则 s t r [ i ] str[i] str[i]入栈
- 循环如下 : 当栈顶和 s t r [ i ] str[i] str[i]相等
-
- 两个都是大写’O’就什么也不做,并 i + + i++ i++
-
- 两个都是小写’o’就不要 i + + i++ i++,并把 s t r [ i ] str[i] str[i]设置成大写’O’
string stk;
for(int i=0; i<n; ) { //注意这里不要i++if(!stk.empty() && str[i]=='o' && str[i]==TOP) {stk.pop_back();str[i] = 'O';} else if(!stk.empty() && str[i]=='O' && str[i]==TOP) {stk.pop_back();i ++;} else {stk.push_back(str[i]);i ++;}
}
完整代码如下
#define debug
#ifdef debug
#include
#include "/home/majiao/mb.h"
#endif#include
#include
#include
#include
#include
#include
#include
#include
#include #define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)using namespace std;#ifdef debug
#define show(x...) \
do { \cout << "\033[31;1m " << #x << " -> "; \err(x); \
} while (0)void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
#endif#ifndef debug
namespace FIO {template <typename T>void read(T& x) {int f = 1; x = 0;char ch = getchar();while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }x *= f;}
};
using namespace FIO;
#endifint n, m, Q, K;#define TOP (stk.back())int main() {
#ifdef debugfreopen("test", "r", stdin);clock_t stime = clock();
#endifstring str;while(cin >> str) {n = (int)str.length();string stk;for(int i=0; i<n; ) {if(!stk.empty() && str[i]=='o' && str[i]==TOP) {stk.pop_back();str[i] = 'O';} else if(!stk.empty() && str[i]=='O' && str[i]==TOP) {stk.pop_back();i ++;} else {stk.push_back(str[i]);i ++;}}cout << stk << endl;}#ifdef debugclock_t etime = clock();printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif return 0;
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
