3 [(]) (]) ([[]()])
No No Yes
#include<iostream> #include<cstdio> #include<stack> #include<algorithm> using namespace std; stack <char> s; int main() { int n; cin>>n; getchar(); while(n--) { while(!s.empty()) s.pop(); int flag=1; char temp; while(1) { temp=getchar(); if(temp==‘\n‘) break; else if(temp==‘[‘||temp==‘(‘) s.push(temp); else if(temp==‘]‘) { if(!s.empty()&&s.top()==‘[‘) s.pop(); else flag=0; } else if(temp==‘)‘) { if(!s.empty()&&s.top()==‘(‘) s.pop(); else flag=0; } } if(s.empty()&&flag) cout<<"Yes"<<endl; else cout<<"No"<<endl; } return 0; }
原文:http://www.cnblogs.com/tt-t/p/5022596.html