UVa 673 Parentheses Balance

时间:2023-03-09 09:58:26
UVa 673 Parentheses Balance

一个匹配左右括号的问题

/*UVa 673 Parentheses Balance*/
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<stack>
using namespace std;
char c[500];
int n;
int pd(char q,char e){
if(q=='(' && e==')')return 1;
if(q=='[' && e==']')return 1;
return 0;
}
int main(){
scanf("%d\n",&n);
while(n--){
gets(c);
if(strcmp(c,"\n")==0){
printf("Yes");
continue;
}
int flag=0;
stack<char>st;
int L=strlen(c);
for(int i=0;i<L;i++){
if(c[i]=='(' || c[i]=='[') st.push(c[i]);
else{
if(st.empty()){
flag=1;
break;
}
if(pd(st.top(),c[i])==1) st.pop();
else{
flag=1;
break;
}
}
}
if(flag==1 || !st.empty())printf("No\n");
else printf("Yes\n");
}
return 0;
}