其他分享
首页 > 其他分享> > 紫书刷题 UVA 673 平衡的括号(Parentheses Balance)

紫书刷题 UVA 673 平衡的括号(Parentheses Balance)

作者:互联网

题目:https://vjudge.net/problem/UVA-673

思路

和简单的一道栈的题,但我还是WA了两次。

代码:

#include <cstdio>
#include <iostream>
#include <stack>
#include <sstream>
using namespace std;

int main()
{
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif

	stack<char> sta;
	string line;
	getline(cin, line);
	stringstream ss(line);
	int t; ss >> t;
	while (t--)
	{
		int flag = true;
		while (!sta.empty()) sta.pop();
		string s;
		getline(cin, s);
		for (int i = 0; i < s.length(); i++) {
			if (s[i] == ' ') continue;
			if (s[i] == '(' || s[i] == '[') sta.push(s[i]);
			else{
				if (sta.empty()) { flag = false; break; }
				if (s[i] == ')' && sta.top() == '(') sta.pop();
				else if (s[i] == ']' && sta.top() == '[') sta.pop();
				else { flag = false; break; }
			}
		}

		if (!sta.empty()) flag = false;

		if (flag) cout << "Yes" << endl;
		else cout << "No" << endl;
	}

	return 0;
}

标签:Parentheses,false,sta,int,pop,673,flag,UVA,include
来源: https://blog.csdn.net/qq_33512762/article/details/115097304