这道题目要求判断括号是否平衡。由于是判断就很简单了,利用stl里面的stack栈,遇到‘(’或者‘[’就入栈,遇到']'或')'就比较出栈。(注意下可能输入空字符)
#include <cstdio>
#include <iostream>
#include <cstring>
#include <stack>
#include <sstream>
#include <algorithm>
#define maxn 200
using namespace std;
int main()
{
// freopen("input.txt","r",stdin);
int n,oks;
char nc,c,str[maxn];
stack<char> s;
cin>>n;
getchar();
while(n--){
while(!s.empty()) s.pop();
oks=1;
gets(str);
stringstream ss(str);
while(ss>>c){
if(c=='['||c=='(') s.push(c);
else if(c==']'){
if(!s.empty()){
nc=s.top();
s.pop();
}
else{
oks=0;
break;
}
if(nc!='['){
oks=0;
break;
}
}
else if(c==')'){
if(!s.empty()){
nc=s.top();
s.pop();
}
else{
oks=0;
break;
}
if(nc!='('){
oks=0;
break;
}
}
}
if(!s.empty()) oks=0;
if(oks) printf("Yes\n");
else printf("No\n");
}
return 0;
}