#include <iostream>
#include <stack>
#include <map>
using namespace std;

int main() {
    int t;
    cin>>t;
    cin.ignore();
    map<char,int> mp = {
        {'{',1},
        {'}',2},
        {'[',3},
        {']',4},
        {'(',5},
        {')',6},
        {'<',7},
        {'>',8}
    };
    while(t--){
        string str;
        getline(cin,str);
        stack<char> a;
        bool jud = true;
        for(char temp : str){
            switch(mp[temp]){
                case 1:
                    if(a.empty()||a.top() == '{') a.push(temp);
                    else jud = false;
                    break;
                case 2:
                    if(a.empty()||a.top() != '{'){
                        jud = false;
                    }else a.pop();
                    break;
                case 3:
                    if(a.empty()||a.top() == '{'||a.top() == '[') a.push(temp);
                    else jud = false;
                    break;
                case 4:
                    if(a.empty()||a.top() != '[') jud = false;
                    else a.pop();
                    break;
                case 5:
                    if(a.empty()||a.top() == '['||a.top() == '{'||a.top() == '(') a.push(temp);
                    else jud = false;
                    break;
                case 6:
                    if(a.empty()||a.top() != '(') jud = false;
                    else a.pop();
                    break;
                case 7:
                    if(a.empty()||a.top() == '('||a.top() == '['||a.top() == '{'||a.top() == '<') a.push(temp);
                    else jud = false;
                    break;
                case 8:
                    if(a.empty()||a.top() != '<') jud = false;
                    else a.pop();
            }
            if(jud == false) break;
        }
        if(!a.empty()) jud = false;
        if(jud) cout<<"YES"<<'\n';
        else cout<<"NO"<<'\n';
    }
    return 0;
}

需要注意的点(我的代码交给ai分析总是改不对,自己后面发现错误的地方):

题目中“从外到内的括号嵌套顺序为:大括号 →→ 中括号 →→ 小括号 →→ 尖括号"

这句话很容易让人(包括ai)误解为’{‘下一级只能嵌套’[','['下一级只能嵌套‘(’,但是:

1.可以同级嵌套,比如'{'里面还可以嵌套一个'{'

2.可以越级嵌套,比如'{'里面不止可以直接嵌套'[',还可以直接嵌套'('、‘<'...