UVa-673-Parentheses Balance
AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 2. Data Structures :: Lists
// 673 - Parentheses Balance #include <iostream> #include <cstdio> #include <cstring> #include <stack> using namespace std; int main(void) { int i, j, n, len; char ch[150]; while(cin >> n) { getchar(); for(i=0; i<n; i++) { stack<char> s; cin.getline(ch, 150); len = strlen(ch); for(j=0; j<len; j++) { if(ch[j]=='(' || ch[j]=='[') s.push(ch[j]); else if( ( s.empty() && (ch[j]==')'||ch[j]==']') ) || ( s.top()=='(' && ch[j]==']' ) || ( s.top()=='[' && ch[j]==')' ) ) break; else if( ( s.top()=='(' && ch[j]==')' ) || ( s.top()=='[' && ch[j]==']' ) ) s.pop(); } if(s.empty() && j==len) cout << "Yes" << endl; else cout << "No" << endl; } } return 0; }