正文
增加 6 行删除 6 行
11 行未改动1213141516171819202122232425262728293031323334353637383940414211 行未改动int main() {string s;cin >> s;stack<char> st;for (char c : s) {if (!st.empty() && st.top() == c) {st.pop(); // 消除当前字符与栈顶字符} else {st.push(c);}}// 构建结果字符串(注意栈的顺序是反的)string result;while (!st.empty()) {result += st.top();st.pop();}// 反转字符串以获得正确顺序reverse(result.begin(), result.end());cout << result << endl;return 0;}我们用栈模拟程序,如果栈顶元素与当前元素相当,删除栈顶元素,不相等就入栈。最后还要反转栈题目讲完了,感谢浏览11 行未改动1213141516171819202122232425262728293031323334353637383940414211 行未改动int main() {string s;cin >> s;stack<char> st;for (char c : s) {if (!st.empty() && st.top() == c) {st.pop(); // 消除当前字符与栈顶字符} else {st.push(c);}}// 构建结果字符串(注意栈的顺序是反的)string result;while (!st.empty()) {result += st.top();st.pop();}// 反转字符串以获得正确顺序reverse(result.begin(), result.end());cout << result << endl;return 0;}我们用栈模拟程序,如果栈顶元素与当前元素相当,删除栈顶元素,不相等就入栈。最后还要反转栈题目讲完了,感谢浏览