--- title: "字符串" created: 2025-11-28 tags: - 算法 --- # 字符串 ## 题目 [字符串](https://www.acwing.com/problem/content/4723/) ![[image-0c5545ac.png]] ## 思路分析 有点像纸牌的小猫钓鱼玩法 直接用栈模拟 发现新元素与栈顶相同就出栈 不同就入栈 ## 代码实现 ```cpp #include using namespace std; int main(){ string s; cin >> s; stack 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; } ``` ## 同类题型 ## 视频讲解 --- ⬅️ [[奶牛慢跑|奶牛慢跑]] 🏠 [[00-刷题理模型]] ➡️ [[弹出序列|弹出序列]]