题解 | #点击消除#

点击消除

https://www.nowcoder.com/practice/8d3643ec29654cf8908b5cf3a0479fd5

#include <cstdio>
#include <iostream>
using namespace std;
#include <stack>
int main() {
   string a;
   cin>>a;   
  stack<char> sta,stb;
  //遍历字符串a;
  for (auto b :a) {
          //当栈不为空且下个字符与栈顶相等消去,否则入栈
          if(!sta.empty()&&b == sta.top()){
          sta.pop();
          } else
          {
          sta.push(b);
  }
  }
//栈空返回0
  if (sta.empty()) {
    cout<<0;
  
  }else //存入新栈在输出保证顺序
  {
    while (!sta.empty()) {

      stb.push(sta.top()) ;
       sta.pop();
       }

    
    }
   while(!stb.empty()){
    cout<<stb.top();
    stb.pop();
   }
  


}

全部评论

相关推荐

Cl_Wg:看牛客匿名贴容易抑郁,白菜就是我的天花板
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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