# UVa 673 - Parentheses Balance ## Online Judge ![](https://i.imgur.com/d0lG8bW.png) ## 解題思路 蠻簡單的一題,23和33行加入counter,是因為已知道答案,所以跳出for loop。 p.s.少了那邊的判斷會多60ms ## 解題中出現的bug 沒什麼大問題。 ## Code ```cpp= #include <iostream> #include <string> #include <stack> using namespace std; int main() { int test_case; cin >> test_case; getchar(); while(test_case--) { bool counter = true; string token; stack<char> Stack; getline(cin, token); for (size_t i = 0; i < token.size()&&counter==true; i++) { if (token[i] == ')'&&!Stack.empty()) { if (Stack.top() == '(') Stack.pop(); else //keep quickly break this for loop { counter = false; break; } } else if (token[i] == ']'&& !Stack.empty()) { if(Stack.top() == '[') Stack.pop(); else { counter = false; break; } } else Stack.push(token[i]); } if (Stack.empty()) cout << "Yes" << endl; else cout << "No" << endl; } return 0; } ``` ###### tags: `UVA code` `cpp` `林基成-C++` `Awwwolf的刷題之路`