UVa 673 (括号配对) Parentheses Balance
2015-02-03 23:44
375 查看
本来是当做水题来做的,后来发现这道题略坑。
首先输入的字符串可能是空串,所以我用了gets函数,紧接着就被scanf("%d", &n)后面的换行符坑掉了。
于是乎再加一句getchar()
代码君
首先输入的字符串可能是空串,所以我用了gets函数,紧接着就被scanf("%d", &n)后面的换行符坑掉了。
于是乎再加一句getchar()
#include <cstdio> #include <stack> #include <cstring> using namespace std; const int maxn = 150; char s[maxn]; bool ok(const char& c1, const char& c2) { if(c1 == '(' && c2 == ')') return true; if(c1 == '[' && c2 == ']') return true; return false; } int main() { //freopen("in.txt", "r", stdin); int n; scanf("%d", &n); getchar(); while(n--) { gets(s); stack<char> S; while(!S.empty()) S.pop(); int l = strlen(s); if(l % 2 != 0) { puts("No"); continue; } bool flag = true; for(int i = 0; i < l; ++i) { if(s[i] == '(' || s[i] == '[') S.push(s[i]); else { if(S.empty()) { flag = false; break; } else if(ok(S.top(), s[i])) S.pop(); else { flag = false; break; } } } if(!S.empty()) flag = false; printf("%s\n", flag ? "Yes" : "No"); } return 0; }
代码君
相关文章推荐
- UVa 673 Parentheses Balance(括号配对 栈)
- Uva 673 Parentheses Balance 平衡的括号
- UVa OJ Parentheses Balance 673 括号平衡
- (平衡的括号)Parentheses Balance UVA - 673
- 习题6-1 平衡的括号(Parentheses Balance, UVa 673)
- 平衡的括号(Parentheses Balance,UVa 673)
- UVA673:Parentheses Balance(平衡的括号)
- [UVa 673] 平衡的括号(Parentheses Balance)
- 平衡的括号(Parentheses Balance, UVa 673)
- UVA 673 Parentheses Balance(括号匹配)
- uva 673 Parentheses Balance (平衡的括号)堆栈模拟
- Parentheses Balance UVA - 673
- UVA 673 Parentheses Balance
- UVa 673 Parentheses Balance
- 【UVa 673】Parentheses Balance
- UVA 673 Parentheses Balance
- UVa 673 Parentheses Balance
- UVa 673 - Parentheses Balance
- uva 673 Parentheses Balance(栈)
- UVA - 673 Parentheses Balance