题解 | #点击消除#

点击消除

https://www.nowcoder.com/practice/8d3643ec29654cf8908b5cf3a0479fd5

#include <iostream>
#include <algorithm>
#include <stack>

using namespace std;

int main() {
    string a;
    stack<char> stk;
    cin >> a;
    for (int i = 0; i < a.size(); i++) {
        if (stk.empty()) {
            stk.push(a[i]);
        }
        else {
            if (stk.top() == a[i]) {
                stk.pop();
            }
            else {
                stk.push(a[i]);
            }
        }
    }
    if (stk.empty())
        cout << 0 << endl;
    else {
        int n = stk.size();
        char b[n];
        for (int i = n-1; i >= 0; i--) {
            b[i] = stk.top();
            stk.pop();
        }
        for (int i = 0; i < n; i++) {
            cout << b[i];
        }
    }
    return 1;
}

全部评论

相关推荐

02-11 12:20
门头沟学院 Java
面试中的青提很胆小:我不信有比我们学校更逆天的,计算机专业就业第一位是我们学校二餐厅的打印店
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务