思路:栈,“(""[“入栈”)“”]“出栈 栈为空时”]"")"入栈 最后判断栈是否为空
<span style="font-size:14px;">#include <cstdio>
#include <stack>
using namespace std;
int main()
{
int n;
stack<char> sta;
scanf("%d%*c", &n);
while (n--) {
// init
while (!sta.empty()) {
sta.pop();
}
// enter
char ch;
while ((ch = getchar()) != '\n') {
if (ch == '(' || ch == '[') {
sta.push(ch);
} else if (ch == ']' || ch == ')'){
if (sta.empty()) {
sta.push(ch);
}
if (ch == ']') {
if (sta.top() != '[') {
} else {
sta.pop();
}
} else {
if (sta.top() != '(') {
} else {
sta.pop();
}
}
}
}
if (sta.empty()) {
printf("Yes\n");
} else {
printf("No\n");
}
}
return 0;
} </span>