栈的使用
Problem E
http://www.nowcoder.com/questionTerminal/3bad4a646b5b47b9b85e3dcb9488a8c3
#include<stack> #include<iostream> #include<string> using namespace std; int main() { int n,i,flag; while(cin>>n) { while(n--) { stack<char> a; string s; cin>>s; for(i=0,flag=0;i<s.length();i++) { if(s[i]=='['||s[i]=='('||s[i]=='{') a.push(s[i]); else if(s[i]==']') { if(!a.empty()) { if(a.top()=='[') a.pop(); else{ flag=1; break; } } else{ flag=1; break; } } else if(s[i]==')') { if(!a.empty()) { if(a.top()=='(') a.pop(); else{ flag=1; break; } } else{ flag=1; break; } } else if(s[i]=='}') { if(!a.empty()) { if(a.top()=='{') a.pop(); else{ flag=1; break; } } else{ flag=1; break; } } } if(!a.empty()) flag=1; if(flag==0) printf("yes\n"); else printf("no\n"); } } return 0; }(•̀ᴗ•́)و