题解 | 吐泡泡

吐泡泡

https://www.nowcoder.com/practice/f86fa2221c094b3d8d1fc79bae450d96

#include<bits/stdc++.h>
using namespace std;

int T;
string s;

int main(){
	cin>>T;
	
	while(T--){
		cin>>s;
		stack<char> st;
		for(int i=0;i<s.size();i++){
			if(s[i]=='o'&&(st.empty()||!st.empty()&&st.top()!='o')){
				st.push('o');
			}else if(s[i]=='o'&&!st.empty()&&st.top()=='o'){
				st.pop();
				if(!st.empty()&&st.top()=='O'){
					st.pop();
				}else{
					st.push('O');
				}
			}else if(s[i]=='O'&&(st.empty()||!st.empty()&&st.top()!='O')){
				st.push('O');
			}else if(s[i]=='O'&&!st.empty()&&st.top()=='O'){
				st.pop();
			}
		}
		string res="";
		while(!st.empty()){
			res=st.top()+res;
			st.pop();
		}
		cout<<res<<endl;
	}
	
    return 0;
}


全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务