#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int Check(char str[],int len)
{
int top,i;
int flag=0;
char temp;
char a[10000];
top=0;
for(i=0;i<len;i++)
{
if(str[i]=='(')
{
a[++top]=str[i];
continue;
}
if(str[i]==')')
{
temp=a[top];
if(temp=='(')
{
flag=1;
top--;
continue;
}
else
{
flag=0;
break;
}
}
if(str[i]=='[')
{
a[++top]=str[i];
continue;
}
if(str[i]==']')
{
temp=a[top];
if(temp=='[')
{
flag=1;
top--;
continue;
}
else
{
flag=0;
break;
}
}
}
if(flag&&(top==0))
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int n,i;
scanf("%d",&n);
getchar();
int count=1;
int len;
int result[5];
while(count<=n)
{
char str[10000];
gets(str);
len=strlen(str);
result[count]=Check(str,len);
count++;
}
for(i=1;i<count;i++)
{
if(result[i])
{
printf("yes\n");
}
else
{
printf("no\n");
}
}
return 0;
}
机试题2019夏令营-5--括号匹配
最新推荐文章于 2021-09-02 13:05:06 发布