Skip to content

Instantly share code, notes, and snippets.

@dada8397
Created February 18, 2017 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dada8397/41a961cfd656784ecaebc7400b5040a1 to your computer and use it in GitHub Desktop.
Save dada8397/41a961cfd656784ecaebc7400b5040a1 to your computer and use it in GitHub Desktop.
UVa 673 - Parentheses Balance
#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