题目
分析
简单的栈的运用,注意输入空行时的处理。
代码
#include <cstdio>
#include <cstring>
#include <stack>
using std::stack;
bool solve(char* s, int l)
{
stack<char> p;
for (int i = 0; i < l; i++) {
if (*(s+i) == '(' || *(s+i) == '[') {
p.push(*(s+i));
} else if (*(s+i) == ')') {
if (!p.empty() && p.top() == '(') p.pop();
else return false;
} else {
if (!p.empty() && p.top() == '[') p.pop();
else return false;
}
}
return p.empty();
}
int main()
{
int t;
char buf[130];
scanf("%d\n", &t);
while (t--) {
fgets(buf, 130, stdin);
printf(solve(buf, strlen(buf)-1) ? "Yes\n" : "No\n");
}
return 0;
}