没什么好说的,,是左括号进栈,右括号判断,就是容易出错注意细节
#include <iostream>
#include <vector>
#include <cstdio>
#include <stack>
using namespace std;
int n;
stack<char> s;
vector<char> vec;
char sign;
int main()
{
cin>>n;
getchar();//排除输入n的回车对后续输入的干扰
while(n--){
int flag = 1;
while((sign=cin.get())!='\n'){
vec.push_back(sign);
}
for(int i=0;i<vec.size();i++){
if(vec[i]=='('||vec[i]=='['){
s.push(vec[i]);
continue;
}
if(s.empty()){
flag=0;
break;
}
char stop = s.top();
if(stop=='('&&vec[i]==')'||stop=='['&&vec[i]==']'){
s.pop();
}
else {
flag = 0;
break;
}
}
if(flag==1&&s.empty())cout<<"Yes"<<endl;
else cout<<"No"<<endl;
while(!s.empty())
s.pop();
vec.clear();
}
return 0;
}