1.小欧的括号运算

https://www.nowcoder.com/questionTerminal/0bcbd996fe234e25960abf0c82327dc2?orderByHotValue=1&page=1&onlyReference=false

#include <iostream>
#include <stack>
using namespace std;

int main() {
    string str;
    cin >> str;
    stack<char> stk;
    for (int i = 0; i < str.size(); i++) {
        if (str[i] == '(') {
            stk.push(str[i]);
        } else {
            if (!stk.empty()&&stk.top() == '(') {
                stk.pop();
                while (!stk.empty()&&stk.top() == '(') {
                    stk.pop();
                }
                    stk.push('(');
            } else {
                stk.push(str[i]);
            }
        }
    }
    cout << stk.size()  << endl;
}
// 64 位输出请用 printf("%lld")

 

posted @ 2025-06-17 16:55  最近饭吃的很多  阅读(13)  评论(0)    收藏  举报