// UVa 673 - Parentheses Balance
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main() {
int cases;
cin >> cases;
string c;
getline(cin, c);
for (; cases; cases--) {
stack<char> s;
getline(cin, c);
bool bad = false;
for (int i = 0; i < c.length(); i++) {
if (c[i] == '(' || c[i] == '[')
s.push(c[i]);
else {
if (s.size() == 0) {
bad = true;
break;
}
char top = s.top();
if ((c[i] == ')' && top != '(') || (c[i] == ']' && top != '[')) {
bad = true;
break;
}
s.pop();
}
}
if (s.size() > 0)
bad = true;
cout << (bad ? "No" : "Yes") << endl;
}
return 0;
}
Thursday, June 11, 2015
UVa 673 - Parentheses Balance
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment