题解 | 吐泡泡

吐泡泡

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


#include<iostream>
using namespace std;
#include<string>
#include<stack>
int main(){
     int n;
    cin >> n;
    while (n--) {
        string s;
        cin>>s;
        stack<char>sta;
        for(int i=0;i<s.size();i++){
            if(s[i]=='o'){
                if(sta.empty()||sta.top()!=s[i]){
                    sta.push(s[i]);
                }else if(!sta.empty()&&sta.top()==s[i]){
                            sta.pop();
                            if(!sta.empty()&&sta.top()=='O'){
                                sta.pop();
                            }
                            else{
                            sta.push('O');
                            }
                }
            }
            if(s[i]=='O'){
                if(sta.empty()||sta.top()!=s[i]){
                    sta.push('O');
                }
                else if(!sta.empty()&&sta.top()==s[i]){
                    sta.pop();
                }
            }
        }
        stack<char>temp;
        while(!sta.empty()){
            temp.push(sta.top());
            sta.pop();
        }
         while(!temp.empty()){
            cout<<temp.top();
            temp.pop();
        }
       cout<<endl;
    }    
}

// #include <iostream>
// using namespace std;
// #include<string>
// #include<stack>
// int main() {
//     int n;
//     cin >> n;
//     while (n--) {
//         string s;
//         stack<char>small;
//         stack<char>big;
//         cin >> s;
//         for (int i = 0; i < s.size(); i++) {
//             if (s[i] == 'o') {
//                 if (small.empty()) {
//                     small.push('o');
//                 } else {
//                     small.pop();
//                     if(big.empty()){
//                         big.push('O');
//                     }
//                     else{
//                         big.pop();
//                     }
//                 }
//             } else if (s[i] == 'O') {
//                 if (big.empty()) {
//                     big.push('O');
//                 } else {
//                     big.pop();
//                 }
//             }
//         }
//         while (!small.empty() || !big.empty()) {  
//             if(!small.empty()){
//                 cout << small.top();
//                 small.pop();
//                 }
//              if(!big.empty()){
//                 cout << big.top();
//                 big.pop();
//                 }
           
//     }
// cout<<endl;
// }
// }
// 64 位输出请用 printf("%lld")





全部评论

相关推荐

程序员牛肉:继续沉淀吧同学,你这就是纯纯的流水线产品。 差不多的学历+两个烂大街项目。自身学历又不行,现在找啥实习呢。有点太浮躁了。多花点心思搞搞ai,开源和八股。这比你这段时间捣鼓一段小厂实习要好得多;
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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