字符串
题目 字符串
思路分析
有点像纸牌的小猫钓鱼玩法
直接用栈模拟
发现新元素与栈顶相同就出栈
不同就入栈
代码实现
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s;
stack<char> stk;
for (auto c: s){
if (!stk.empty() && stk.top() == c)
stk.pop();
else
stk.push(c);
}
string res = "";
while (!stk.empty()){
res.push_back(stk.top());
stk.pop();
}
reverse(res.begin(),res.end());
cout << res << endl;
return 0;
}
💬 评论