#include <bits/stdc++.h> using namespace std; int bracketNum(char c) { if (c == '(')return 1; if (c == ')')return -1; if (c == '[')return 2; if (c == ']')return -2; if (c == '{')return 3; if (c == '}')return -3; return 100; } int main() { string nn;getline(cin,nn); int n=stoi(nn); while (n--) { string s;getline(cin,s); for (auto it = s.begin(); it != s.end();) { if (bracketNum(*it) == 100)it = s.erase(it); else it++; } // cout<<s<<" "; if(s.length()==0){ cout<<"yes"<<endl; }else{ stack<char>stk; bool isRight = true; for(int i =0;i<s.length();i++){ if(bracketNum(s[i])>0){ //左括号都入栈 stk.push(s[i]); }else{ //碰到右括号 if(stk.size()==0){ isRight=false; break; }else if(bracketNum(s[i])+bracketNum(stk.top()) != 0){ isRight=false; break; } stk.pop(); } } cout<<(isRight&&stk.size()==0?"yes":"no")<<endl; } } } // 64 位输出请用 printf("%lld")
其中有一个测试用例是(((,注意判断出循环之后栈是否为空,不为空就是no