此题比较简单,用stack来解析即可。
#include <iostream>
#include <algorithm>#include <string>
#include <stack>
#include <cstring>
#include <cstdio>
using namespace std;
const int xsize = 128 + 10;
stack<char > inp;
char tin[xsize];
int main(){
int n;
scanf("%d", &n);
getchar();
for(int i = 0; i < n; ++ i){
gets(tin);
int len = strlen(tin);
for(int i = 0; i < len; ++i){
if( !inp.empty() && ((tin[i] == ']' && inp.top() == '[') || (tin[i] == ')' && inp.top() == '(')) )
inp.pop();
else if(tin[i] == '[' || tin[i] == ']' || tin[i] == '(' || tin[i] == ')')
inp.push(tin[i]);
}
if(inp.empty())
cout <<"Yes" <<endl;
else
cout <<"No" <<endl;
while(!inp.empty())
inp.pop();
}
return 0;
}