文章

75

粉丝

0

获赞

147

访问

8.7k

头像
括号匹配 题解:c++
P1501 西北工业大学机试题
发布于2026年2月25日 14:38
阅读数 28

#include <bits/stdc++.h>
using namespace std;

stack<char> S;
int main(){
    string str;
    cin >> str;
    for(int i = 0; i < str.size(); i++){
        if(S.empty())
            S.push(str[i]);
        else{
            if((S.top() == '(' && str[i] == ')') || (S.top() == '[' && str[i] == ']'))
                S.pop();
            else
                S.push(str[i]);
        }
    }
    if(S.empty()) cout << "YES";
    else cout << "NO";
    return 0;
}

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发