#include #include #define MAXSIZE 100 #define OK 1 #define ERROR 0 #define OVERFLOW -2 using namespace std; typedef struct { char *base; char *top; int stacksize; }SqStack; int InitStack(SqStack &S) {//栈初始化 S.base=new char[MAXSIZE]; if(!S.base) return OVERFLOW; S.top=S.base; S.stacksize=MAXSIZE; return OK; } int Push(SqStack &S, char e) {//入栈 if(S.top-S.base==S.stacksize) //栈满 return ERROR; *S.top=e; S.top++; return OK; } int Pop(SqStack &S) {//出栈返回栈顶元素 if(S.top==S.base) //栈空 return ERROR; S.top--; return *S.top; } int IsPalindrome(SqStack &S,char *t) {//括号匹配序列 int count=0; char *p=t; while(*p!=0) { count++; p++; } for(int i=0;i<count;i++) { if(t[i]=='(') { Push(S,t[i]); } else if(t[i]==')') { if(*S.top=='(') Pop(S); else return ERROR; } } if(S.top==S.base) // 栈为空 return OK; else return ERROR; } int main() { char t[100]; while(cin>>t&&t[0]!='0') { SqStack S; InitStack(S); if(IsPalindrome(S,t)==1) cout<<'YES'<<endl; else cout<<'NO'<<endl; }

return 0;

原文地址: https://www.cveoy.top/t/topic/pigo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录