/* coder:ACboy date: 2010-3-7 result: AC description: UVa 673 Parentheses Balance */ #include <iostream> using namespace std; char stack[128]; int top; int match(char & a, char & b) { if ((a == '(' && b == ')') || (a == '[' && b == ']')) { return 1; } else return 0; } int main() { int n; #ifndef ONLINE_JUDGE freopen("673.txt", "r", stdin); #endif scanf("%d/n", &n); while (n--) { char input[128] = {0}; gets(input); if (input == "") { cout << "Yes" << endl; continue; } top = 0; int len = strlen(input); for (int i = 0; i < len; i++) { if (input[i] == '(' || input[i] == '[') { stack[top++] = input[i]; } else if (input[i] == ')' || input[i] == ']') { if (match(stack[top - 1], input[i])) { top--; } else { stack[top++] = input[i]; } } } if (top == 0) cout << "Yes" << endl; else cout << "No" << endl; } return 0; }