题解 | #点击消除#
点击消除
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();
}
}
三奇智元机器人科技有限公司公司福利 88人发布