文章

61

粉丝

98

获赞

17

访问

16.7k

头像
括号匹配 题解:c++ stack/map实现
P1501 西北工业大学2015机试题
发布于2024年3月16日 11:47
阅读数 137

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

map<char, char> bkmatch = {{')', '('}, {']', '['}};
bool match(string s);

int main(){
    string s;
    cin >> s;
    if(match(s))
        cout << "YES";
    else
        cout << "NO";
    return 0;
}


bool match(string s){
    stack<char> stk;
    int slen = s.size();
    for(int i = 0; i < slen; i++){
        if(s[i] == '(' || s[i] == '['){      //左括弧,入栈
            stk.push(s[i]);
        }else if(s[i] == ')' || s[i] == ']'){     //右括弧,出栈
            if(stk.empty())     return false;       //栈空无左括号可出
      &...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发