括号配对问题
Time Limit: 3000ms
Memory Limit: 128000KB
64-bit integer IO format: Java class name:
现在,有一行括号序列,请你检查这行括号是否配对。
Input
第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于10000,且S不是空串),测试数据组数少于5组。数据保证S中只含有"[","]","(",")"四种字符
Output
每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No
Sample Input
3 [(]) (]) ([[]()])
Sample Output
No No Yes
#include<stdio.h>
#include<string.h>
char a[10005]={0},b[10005];
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int len,top=-1;
scanf("%s",a);
len=strlen(a);
for(int i=0; i<len; i++)
{
if(top==-1)
{
//printf("%c %s\n",a[i],b);
b[++top]=a[i];
continue;
}
if(top>-1&&a[i]==']'&&b[top]=='['||top>-1&&a[i]==')'&&b[top]=='(')
{
//printf("%c %s\n",a[i],b);
top--;
continue;
}
else
{
//printf("%c %s\n",a[i],b);
b[++top]=a[i];
continue;
}
}
if(top>-1)
{
printf("No\n");
}
if(top==-1)
{
printf("Yes\n");
}
}
return 0;
}