文章

17

粉丝

0

获赞

5

访问

1.3k

头像
括号匹配 题解:
P1501 西北工业大学机试题
发布于2025年5月17日 16:11
阅读数 64

//1067题的代码稍微改一点就行。主要两点不同:①只有两个类型的括号②括号没有优先级
//解题思路:map映射字符为整数,结合数据结构的stack进行括号匹配。 
#include<iostream>
#include<map>
#include<stack>
using namespace std;

map<char,int> Bracket={{'[',-2},{'(',-1},{')',1},{']',2}};

bool match(const string &str){
    bool flag=true;
    stack<char> S;
    for(int i=0;i<str.length();i++){
        if(S.empty()){
            if(Bracket[str[i]]>0){
                flag=false;
                break;
            }
            else    S.push(str[i]);
        }
       &nb...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发