#include <bits/stdc++.h>
#define status int
#define OK 1
#define FAIL 0
#define ERROR 0
#define Elemtype char
#define INIT_STACK_SIZE 100
#define STACKINCREMENT 10
using namespace std;
typedef struct {
Elemtype *top;
Elemtype *base;
int stacksize;
}SqStack;
status initstack (SqStack &S){
S.base = (Elemtype *)malloc(INIT_STACK_SIZE *sizeof (Elemtype));
if (!S.base) return ERROR;
S.top = S.base;
S.stacksize = INIT_STACK_SIZE;
return OK;
}
status DestroyStack(SqStack &S){
if (!S.base) return ERROR;
free(S.base);
S.top = NULL;
S.stacksize =0;
return OK;
}
status ClearStack(SqStack &S){
if (S.stacksize == 0) return ERROR;
S.top = S.base;
S.stacksize = 0;
return OK;
}
status StackEmpty(SqStack S){
if (!S.base) return ERROR;
if (S.base == S.top) return OK;
else return 0;
}
int StackLength(SqStack s){
return s.stacksize;
}
status GetTop(SqStack S,Elemtype &e){
if (!S.base) return ERROR;
e = *(S.top-1);
return OK;
}
status Push(SqStack &S,Elemtype e){
if (!S.base) return ERROR;
if (S.top - S.base >= S.stacksize){
S.base = (Elemtype *)realloc (S.base,S.stacksize+STACKINCREMENT);
S.top = S.base + S.stacksize;
S.stacksize +=STACKINCREMENT;
}
*S.top = e;
S.top++;
return OK;
}
status Pop(SqStack &S,Elemtype &e){
if (!S.base || S.top == S.base) return ERROR;
e = *(--S.top);
return OK;
}
status IsEmpty(SqStack S){
if (S.base==S.top) return OK;
else return FAIL;
}
status Match(SqStack &S,char sq[]){
int i=0;
char elem,lastelem;
while (sq[i]!='\n' && i<20){
GetTop(S,lastelem);
if (sq[i]=='(' || sq[i]=='<' || sq[i]=='[') Push(S,sq[i]);
else {
if (lastelem =='(' && sq[i]==')') Pop(S,elem);
else if (lastelem=='<' && sq[i]=='>') Pop(S,elem);
else if (lastelem=='[' && sq[i]==']') Pop(S,elem);
}
++i;
}
if (IsEmpty(S)) return OK;
else return FAIL;
}
int main()
{
SqStack S;
initstack(S);
char sq[20];
memset(sq,'\n',sizeof (sq));
cin>>sq;
if (Match(S,sq)) cout<<"YES";
else cout<<"NO";
}
Match里面只是很简单的逻辑,甚至没有判断嵌套。需要改进