#include<stdio.h> #include<stdlib.h> #define STACK_SIZE 100 #define overflow -2 #define OK 1 #define TRUE 1 #define FALSE 0 typedef char datatype; typedef int Status; typedef struct {datatype *base; datatype *top; int stacksize; } seqstack; void Initial(seqstack *S) { S->base=(datatype*)malloc(STACK_SIZE *sizeof(datatype)); if(!S->base) exit (-1); S->top=S->base; S->stacksize=STACK_SIZE; } Status DestroyStack(seqstack *S) { free(S->base); S->base=NULL; S->top=NULL; S->stacksize=0; return OK; } Status ClearStack(seqstack *S) {S->top=S->base; S->stacksize=STACK_SIZE; return OK; } Status StackEmpty(seqstack *S) { if(S->top==S->base) return TRUE; else return FALSE; } datatype GetTop(seqstack S) {datatype e; e=*(S.top-1); return e; } int IsEmpty(seqstack *S) { return S->top==S->base; } int IsFull (seqstack *S) { return S->top-S->base==STACK_SIZE-1; } void Push(seqstack *S,datatype x) { if(IsFull(S)) { printf("overflow"); exit (1); } else *S->top++=x; } void Pop(seqstack *S) { if(IsEmpty(S)) { printf("NULL"); exit (1); } else --S->top; } datatype Top(seqstack *S) {if(IsEmpty(S)) { printf("empty"); exit (1); } return *(S->top-1); } int match (seqstack *S,char *str) { char x; int i, flag=1; for(i=0;str[i]!='\0';i++) { switch(str[i]) { case '(' : Push(S,'('); break; case '[' : Push(S,'['); break; case '{' : Push(S,'{'); break; case ')' : x=Top(S); Pop(S); if(x!='(') flag=0; break; case ']' : x=Top(S); Pop(S); if(x!='[') flag=0; break; case '}' : x=Top(S); Pop(S); if(x!='{') flag=0; break; } if(!flag) break; } if(IsEmpty(S)==1 && flag) return 1; else return 0; } int main () { int t; scanf("%d%*c",&t); while(t--){ seqstack S,*st; st=&S; char str[100]; Initial(st); gets(str); if(match(st,str)) printf ("ok\n"); else printf ("error\n"); } return 0; }