- #include<bits/stdc++.h>
- using namespace std;
- #define STACK_INIT_SIZE 10000
- #define STACKINCREMENT 10
- #define TRUE 1
- #define FALSE 0
- #define OK 1
- #define ERROR 0
- #define INFEASIBLE -1
- #define OVERFLOW -2
- using namespace std;
- typedef char SElemType, Status;
- typedef struct
- {
- SElemType *base;
- SElemType *top;
- int stacksize;
- }SqStack;
- Status InitStack(SqStack &S)
- {
- S.base = (SElemType *)malloc(sizeof(SElemType)*STACK_INIT_SIZE);
- if (!S.base)
- exit(OVERFLOW);
- S.top = S.base;
- S.stacksize = STACK_INIT_SIZE;
- return OK;
- }
- Status Push(SqStack &S, SElemType e)
- {
- if (S.top - S.base >= S.stacksize)
- {
- S.base = (SElemType*)malloc(sizeof(SElemType)*(S.stacksize + STACKINCREMENT));
- if (!S.base)
- exit(OVERFLOW);
- S.top = S.base + S.stacksize;
- S.stacksize += STACKINCREMENT;
- }
- *S.top++ = e;
- return OK;
- }
- Status Pop(SqStack &S)
- {
- if (S.top == S.base)
- return ERROR;
- S.top--;
- return OK;
- }
- Status GetTop(SqStack &S, SElemType &e)
- {
- if (S.base == S.top)
- return ERROR;
- e = *(S.top - 1);
- return OK;
- }
- Status Empty(SqStack S)
- {
- if (S.top == S.base)
- return OK;
- else
- return ERROR;
- }
- const int maxn = 1000 + 5;
- char s[maxn];
- bool Find(SqStack &S, char ch)
- {
- char tmp[maxn]; //初始化为“\n”;
- memset(tmp, '\n', sizeof(tmp)); //Memset 用来对一段内存空间全部设置为某个字符,一般用在对定义的字符串进行初始化为‘ ’或‘/0’;
- int num = 0;
- while (!Empty)
- {
- SElemType e2;
- GetTop(S, e2);
- if (e2 == ch)
- {
- Pop(S);
- for (int i = num - 1; i >= 0; i--)
- Push(S, tmp[i]);
- return true;
- }
- else
- {
- tmp[num++] = e2;
- }
- Pop(S);
- }
- for (int i = num - 1; i >= 0; i--)
- Push(S, tmp[i]);
- return false;
- }
- void judge(char ch)
- {
- if (ch == '(')
- printf("(-?\n");
- else if (ch == '[')
- printf("[-?\n");
- else if (ch == '{')
- printf("{-?\n");
- else if (ch == '<')
- printf("/*-?\n");
- }
- void fun(SqStack &Sta, char ch)
- {
- int flag = 1;;
- if (!Empty(S)
- {
- SElemType e;
- GetTop(Sta, e);
- if (e == '(')
- Pop(Sta);
- else if (flag)
- {
- printf("NO\n");
- flag = 0;
- judge(e);
- }
- }
- }
- int main()
- {
- SqStack Sta;
- InitStack(Sta);
- int flag = 1;
- while (gets(s))
- {
- if (s[0] == '.') break;
- int len = strlen(s);
- for (int i = 0; i < len; i++)
- {
- if (s[i] == '(' || s[i] == '[' || s[i] == '{')
- Push(Sta, s[i]);
- else if (s[i] == '/'&&s[i + 1] == '*'&&i + 1 < len)
- {
- ++i;
- Push(Sta, '<');
- }
- else if (s[i] == ')')
- {
- if (!Empty(Sta)
- {
- SElemType e;
- GetTop(Sta, e);
- if (e == '(')
- Pop(Sta);
- else if (flag)
- {
- printf("NO\n");
- flag = 0;
- judge(e);
- }
- }
- else if (flag)
- {
- flag = 0;
- printf("NO\n");
- printf("?-)\n");
- }
- }
- else if (s[i] == ']')
- {
- if (!Empty(Sta)
- {
- SElemType e;
- GetTop(Sta, e);
- if (e == '[')
- Pop(Sta);
- else if (flag)
- {
- printf("NO\n");
- flag = 0;
- judge(e);
- }
- }
- else if (flag)
- {
- flag = 0;
- printf("NO\n");
- printf("?-]\n");
- }
- }
- else if (s[i] == '}')
- {
- if (!Empty(Sta)
- {
- SElemType e;
- GetTop(Sta, e);
- if (e == '{')
- Pop(Sta);
- else if (flag)
- {
- printf("NO\n");
- flag = 0;
- judge(e);
- }
- }
- else if (flag)
- {
- flag = 0;
- printf("NO\n");
- printf("?-}\n");
- }
- }
- else if (s[i] == '*'&&s[i + 1] == '/'&&i + 1 < len)
- {
- ++i;
- if (!Empty(Sta)
- {
- SElemType e;
- GetTop(Sta, e);
- if (e == '<')
- Pop(Sta);
- else if (flag)
- {
- printf("NO\n");
- flag = 0;
- judge(e);
- }
- }
- else if (flag)
- {
- flag = 0;
- printf("NO\n");
- printf("?-*/\n");
- }
- }
- }
- }
- if (flag)
- {
- if (!Empty(Sta)
- printf("YES\n");
- else
- {
- SElemType e;
- GetTop(Sta, e);
- printf("NO\n");
- judge(e);
- }
- }
- }