文章

7

粉丝

0

获赞

12

访问

1.3k

头像
括号的匹配 map+stack
P1067 中山大学机试题
发布于2026年2月8日 14:58
阅读数 212

#include<bits/stdc++.h>
using namespace std;
map<char,int> rk={{'{',4},{'[',3},{'(',2},{'<',1}};
int main(){
    int n;
    cin>>n;
    while(n-->0){
        string s;
        cin>>s;
        stack<char> st;
        bool valid=true;
        for(int i=0;i<s.length();i++){
            if(!st.empty()){
                char top=st.top();
                if(s[i]=='{'||s[i]=='['||s[i]=='('||s[i]=='<'){
                    if(rk[s[i]]>rk[top]){
                        valid=false;
            ...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发