吐泡泡

链接:https://ac.nowcoder.com/acm/contest/22669/E

题目描述

小鱼儿吐泡泡,嘟嘟嘟冒出来。小鱼儿会吐出两种泡泡:大泡泡"O",小泡泡"o"。 两个相邻的小泡泡会融成一个大泡泡,两个相邻的大泡泡会爆掉。 (是的你没看错,小气泡和大气泡不会产生任何变化的,原因我也不知道。) 例如:ooOOoooO经过一段时间以后会变成oO。

C++代码

#include <iostream>
#include <vector>
#include <string>
#include <stack>

class mySolution{
public:
    std::string restCircle(std::string bigSmall){
        int lengthOfBigSmall=bigSmall.size();
        std::stack<char> stack;
        stack.push(bigSmall[0]);
        for(int i=1;i<lengthOfBigSmall;i++){
            if(!stack.empty()){
                std::string tempString;
                tempString+=stack.top();
                tempString+=bigSmall[i];
                if(tempString=="oo"){
                    stack.pop();
                    if(!stack.empty()&&stack.top()=='O'){
                        stack.pop();
                    }
                    else{
                        stack.push('O');   
                    }
                }
                else if(tempString=="OO"){
                    stack.pop();
                }
                else{
                    stack.push(bigSmall[i]);
                }
            }
            else{
                stack.push(bigSmall[i]);
            }
        }
        std::string result(stack.size(),' ');
        int size=stack.size()-1;
        while(!stack.empty()){
            result[size--]=stack.top();
            stack.pop();
        }
        return result;
    }    
};

int main(){
    mySolution*solution=new mySolution();
    std::string bigSmall;
    while(std::cin>>bigSmall){
        std::string answer=solution->restCircle(bigSmall);
        std::cout<<answer<<std::endl;
    }
    
    return 0;
}