2016-07-03 Problem Solving►UVa UVa 673 - Parentheses Balance Contents 1. Problem2. Solution3. Code Problem題目網址中文網址 Solution找對應的括號,一遇到 ‘)’ 、 ‘]’ 卻無法有對應的括號,即可停止。 CodeUVa 673123456789101112131415161718192021222324252627282930313233343536373839404142434445#include<cstdio>#include<stack>#define N 130int main(){ std::stack<char> parentheses; int n,i; char s[N] = { NULL }; scanf("%d", &n); getchar(); while (n--) { fgets(s, N, stdin); for (i = 0; s[i] != '\n'; i++) { if (s[i] == '(' || s[i] == '[') parentheses.push(s[i]); else if (s[i] == ')') { if (!parentheses.empty() && parentheses.top() == '(') parentheses.pop(); else break; } else if (s[i] == ']') { if (!parentheses.empty() && parentheses.top() == '[') parentheses.pop(); else break; } } printf("%s\n", parentheses.empty() && s[i] == '\n' ? "Yes" : "No"); while (!parentheses.empty()) parentheses.pop(); } return 0;} Newer UVa 543 - Goldbach's Conjecture Older UVa 140 - Bandwidth