文章

1

粉丝

0

获赞

5

访问

370

头像
括号的匹配 题解:
P1067 中山大学机试题
发布于2025年3月19日 16:27
阅读数 370

#include<iostream>
#include<algorithm>
#include<math.h>
#include<ctype.h>
#include<stack>
#include<stdlib.h>
#include<cstring>
#include<queue>
#include<map>

using namespace std;

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

int main(){
    int n;
    cin >> n;
    while(n > 0){
        string s;
        cin >> s;
        stack<char> sk;
        int len = s.length();
        int flag = 0;
        if(len % 2 != 0) flag = 1;
        else{
            for(int i = 0; i < len; i++){
                char c = s[i];
                if(sk.empty()) sk.push(c); 
                else if(fh[c] < 0 && fh[sk.top()] > 0 && fh[c] + fh[sk.top()] == 0) sk.pop();
                else if(fh[c] > 0 && fh[c] <= fh[sk.top()]) sk.push(c);
                else{
       ...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发