Created
February 18, 2017 08:25
-
-
Save dada8397/41a961cfd656784ecaebc7400b5040a1 to your computer and use it in GitHub Desktop.
UVa 673 - Parentheses Balance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <stack> | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
int main(void) { | |
int n; | |
string s; | |
while(cin >> n) { | |
getchar(); | |
while(n--) { | |
getline(cin, s); | |
stack<char> stk; | |
bool correct = true; | |
for(int i=0; i<s.length() && correct; i++) { | |
switch(s[i]) { | |
case '[': | |
case '(': | |
stk.push(s[i]); | |
break; | |
case ']': | |
if(!stk.empty() && stk.top() == '[') { | |
stk.pop(); | |
break; | |
} else { | |
correct = false; | |
break; | |
} | |
case ')': | |
if(!stk.empty() && stk.top() == '(') { | |
stk.pop(); | |
break; | |
} else { | |
correct = false; | |
break; | |
} | |
default: | |
break; | |
} | |
} | |
if(correct && stk.empty()) { | |
cout << "Yes" << endl; | |
} else { | |
cout << "No" << endl; | |
} | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment