您的位置:首页 > 其它

UVA - 673 Parentheses Balance

2014-09-27 10:55 141 查看
题目大意:给出一系列字符串,求括号是否匹配正确

解题思路:用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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: