69,371
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define MAXN 10001
int push(char *stack,int maxn,int *toppt,char x)
{
if(*toppt>=maxn)
return 1;
stack[*toppt]=x;
++(*toppt);
return 0;
}
int pop(char *stack,int *toppt,char *cp)
{
if(*toppt==0)
return 1;
--(*toppt);
*cp=stack[*toppt];
return 0;
}
void OutputStack(int *stack,int toppt)
{
int i;
for(i=toppt-1;i>=0;i--)
printf("%d",stack[i]);
printf("\n");
}
int correct(char *exp,int max)
{
int flag=0;
char s[MAXN];
int top=0;
char c;
int i;
for(i=0;i<max&&flag==0;i++)
{
if(exp[i]=='('||exp[i]=='['||exp[i]=='{'||exp[i]=='<')
push(s,MAXN,&top,exp[i]);
if(exp[i]==')'||exp[i]==']'||exp[i]=='}'||exp[i]=='>'){
flag=pop(s,&top,&c);
if((exp[i]==')'&&c!='(')||(exp[i]==']'&&c!='[')||(exp[i]=='}'&&c!='{')||(exp[i]=='>'&&c!='<'))
flag=1;
}
}
if(top!=0)
flag=1;
return flag;
}
int main()
{
char s[MAXN],c;
int i,l;
char exp[10001];
int top=0;
while(1)
{
gets(exp);
exp[MAXN]='\0';
if(strcmp(exp,"0")==0)
return 0;
if(correct(exp,strlen(exp))!=0)
printf("NO\n");
else
printf("YES\n");
}
}