

代码:
#include<bits/stdc++.h>
using namespace std;
stack<int> sta;
string s;
int n,t;
bool match(){
cin>>s;
for(int i=0;i<s.size();i++)
{
if(s[i]=='<')t=1;
if(s[i]=='(')t=2;
if(s[i]=='[')t=3;
if(s[i]=='{')t=4;
if(s[i]=='}')t=5;
if(s[i]==']')t=6;
if(s[i]==')')t=7;
if(s[i]=='>')t=8;
if(t>=1 && t<=4){
if(sta.top()>=t || sta.empty())
sta.push(t);
else
return false;
}
if(t>=5 && t<=8)
if(!sta.empty() && sta.top()+t==9)
sta.pop();
else
return false;
}
if(!sta.empty())
return false;
else
return true;
}
int main()
{
cin>>n;
while(n--)
{
while(!sta.empty()) sta.pop();
if(match())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}