#include <stdio.h>
int main()
{
int n, top, j;
char str[10000],stack[5000];
scanf("%d",&n);
while (n--)
{
scanf("%s",str);
if (str[0] == ')' || str[0] == ']')
{
stack[0] = '1';
}
else
{
for (top = 0,j=0; str[j] != '\0';j++)
{
if (str[j] == '(' || str[j] == '[')
{
stack[top] = str[j];
top++;
}
else if (str[j] == ']')
{
if (stack[top-1] != '[')
{
stack[0] = '1';
break;
}
else
{
stack[top-1] = '0';
top--;
}
}
else
{
if (stack[top-1] != '(')
{
stack[0] = '1';
break;
}
else
{
stack[top-1] = '0';
top--;
}
}
}
}
printf(stack[0] == '0'?"Yes\n":"No\n");
}
return 0;
}
数据结构 栈的应用 括号匹配
最新推荐文章于 2023-03-07 12:22:54 发布