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 <time.h>
#include "/home/majiao/mb.h"
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#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;
#endif


int n, m, Q, K;

#define TOP (stk.back())

int main() {
#ifdef debug
	freopen("test", "r", stdin);
	clock_t stime = clock();
#endif

	string 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 debug
	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}





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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值