题解 | #点击消除#
点击消除
https://www.nowcoder.com/practice/8d3643ec29654cf8908b5cf3a0479fd5
#include <iostream>
#include <iterator>
#include <memory>
using namespace std;
#include<stack>
#include<algorithm>
int main() {
// int a, b;
// while (cin >> a >> b) { // 注意 while 处理多个 case
// cout << a + b << endl;
// }
string a;
cin >> a;
stack<char> sta;
for (int i = 0; i < a.size(); i++) //遍历字符串
{
if (sta.empty())
{
sta.push(a[i]); //空栈下进栈
}
else //非空栈
{
if (sta.top() == a[i]) //栈顶与当前字符一样
{
sta.pop(); //出栈
}
else sta.push(a[i]); //不一样就进栈
}
}
if (sta.empty()) cout << 0; //最终空栈输出0
else
{
string str;
while (!sta.empty()) //出栈赋给字符串str
{
str += sta.top();
sta.pop();
}
reverse(str.begin(), str.end()); //反转字符串
cout << str;
}
return 0;
}
// 64 位输出请用 printf("%lld")

查看8道真题和解析