UVA - 673 Parentheses Balance

来源:互联网 发布:刀剑乱舞极化胁差数据 编辑:程序博客网 时间:2024/06/06 19:05

题目大意:给出一系列字符串,求括号是否匹配正确

解题思路:用c++的模板的stack做

看来修改系统还是有bug的,少了else的那个既然还能成功

#include<cstdio>#include<cstring>#include<stack>#include<cstdio>using namespace std;int main() {char str[200];int test;scanf("%d", &test);getchar();for(int i = 0; i < test; i++) {stack<char> q;gets(str);int len = strlen(str);int j;for(j = 0 ; j < len; j++) {if(str[j] == '(' || str[j] == '[')q.push(str[j]);if(str[j] == ')')if(q.empty())break;else if(q.top() == '(')q.pop();//else //break;if(str[j] == ']' )if(q.empty())break;else if(q.top() == '[')q.pop();//else //break;}if(j != len)printf("No\n");else if(q.empty())printf("Yes\n");else printf("No\n");}return 0;}


0 0