#include<iostream> #include<string.h> #include<stack> using namespace std; //判断括号是否能匹配,如果最后栈为空,则括号匹配,否则括号不匹配; int main(){ char ch; stack<char> s; int n; cin>>n; while(n--){ cin>>ch; if(s.empty()||ch==‘[‘||ch==‘(‘){ s.push(ch); } if(ch==‘]‘){ if(s.top()==‘[‘) s.pop(); else s.push(ch); }if(ch==‘)‘){ if(s.top()==‘(‘) s.pop(); else s.push(ch); } } if(s.empty()) cout<<"yes"<<endl; else cout<<"no"<<endl; system("pause"); return 0; }
时间: 2024-10-13 13:41:14