回复 楼主 yshx88
没办法,只能用栈啊
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define Stack_Size 100
#define ElemType char
typedef struct Stack
{
ElemType base[Stack_Size];
int top;
}SqStack;
int InitStack(SqStack *S)
{
S->top=-1;
return OK;
}
int Push(SqStack *S,ElemType e)
{
if(S->top>=Stack_Size-1)
return ERROR;
S->top++;
S->base[S->top]=e;
return OK;
}
int Pop(SqStack *S)
{
if(S->top==-1)
return ERROR;
S->top--;
return OK;
}
char convert(ElemType e)
{
if(e=='{')
e='}';
if(e=='[')
e=']';
if(e=='(')
e=')';
return e;
}
int Check(SqStack *S,ElemType *p)
{
while(*p!='\0')
{
if(*p=='{'||*p=='['||*p=='(')
if(Push(S,convert(*p))==0)
return ERROR;
if(*p=='}'||*p==']'||*p==')')
{
if(*p==S->base[S->top])
{
if(Pop(S)==0)
return ERROR;
}
else
return ERROR;
}
p++;
}
if(S->top==-1)
return OK;
else
return ERROR;
}
void main()
{
int n;
scanf("%d",&n);
while(n--){
SqStack S;
char p[10000];
InitStack(&S);
scanf("%s",p);
if(Check(&S,p)==0)
printf("No\n");
else
printf("Yes\n");
}
}