C++ 因为要顺序打印,所以逆序进栈~
#include <iostream> #include <stack> using namespace std; int main() { string a; cin >> a; stack<char> t; t.push('#'); for (int i=a.size()-1; i>-1; i--) { if (a[i]==t.top()) t.pop(); // 两两相消 不是相同的全消 else t.push(a[i]); } if (t.size()==1) cout << 0; // 打印0 不是return 0 else { while (t.top()!='#') { cout << t.top(); t.pop(); } } } // 64 位输出请用 printf("%lld")