UVA-673-栈-水题

Posted 菜菜

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA-673-栈-水题相关的知识,希望对你有一定的参考价值。

题意:

检测括号是否匹配,注意有空格

#include<stdio.h>
#include<iostream>
#include <strstream>
#include<string>
#include<memory.h>
#include<math.h>
#include<sstream>
#include<queue>
#include<stack>
using namespace std;
struct Node
{
	int r;
	int c;
	int total;
};
const Node dir[] = { { -1, 2 }, { 1, 2 }, { -2, 1 }, { 2, 1 }, { -2, -1 }, { 2,
		-1 }, { -1, -2 }, { 1, -2 } };

int main()
{

	string yes = "Yes";
	string no = "No";
	int n;
	cin >> n;
	string str;
	getline(cin, str);
	while (n--)
	{
		getline(cin, str);
		
		int length = str.length();
		stack<char> s;
		int ok = 1;
		for (int i = 0; i < length; i++)
		{
			char c = str.at(i);
			if (c == ‘ ‘)
				continue;
			if (c == ‘(‘ || c == ‘[‘)
			{
				s.push(c);
			}
			else if (c == ‘)‘ || c == ‘]‘)
			{
				if (s.size() == 0)
				{
					ok = 0;
					break;
				}
				else
				{
					char cc = s.top();
					s.pop();
					if (c == ‘)‘)
					{
						if (cc != ‘(‘)
						{
							ok = 0;
							break;
						}
					}
					else
					{
						if (cc != ‘[‘)
						{
							ok = 0;
							break;
						}
					}
				}
			}
		}
		if (s.size() != 0)
			ok = 0;

		if (ok)
			cout << yes << endl;
		else
			cout << no << endl;
	}
}

  

以上是关于UVA-673-栈-水题的主要内容,如果未能解决你的问题,请参考以下文章

B - Parentheses Balance (UVA - 673)

平衡的括号[UVA-673]

习题 6-1 UVA-673Parentheses Balance

习题6_1 平衡的括号(Parentheses Balance, UVa 673)

习题6_1 平衡的括号(Parentheses Balance, UVa 673)

uva673-Parentheses Balance