Balancing Symbols

Posted KennyRom

tags:

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

symbols匹配问题

#include<iostream>
#include<string>
using namespace std;

struct Node
{
	char data;
	Node*next;
};

struct LinkStack
{
	Node*top;
};

LinkStack*create()
{
	LinkStack*stack = new LinkStack;
	stack->top = NULL;
	return stack;
}

bool isEmpty(LinkStack*stack)
{
	return (stack->top == NULL);
}

void pop(LinkStack*stack)
{
	Node*p = stack->top;
	stack->top = stack->top->next;
	delete p;
}

void push(LinkStack*stack, char item)
{
	Node*p = new Node;
	p->data = item;
	p->next = stack->top;
	stack->top = p;
}

char Top(LinkStack*stack)
{
	return stack->top->data;
}

int main()
{
	string s;
	cin >> s;
	LinkStack*stack = create();
	bool flag = true;

	for (auto i:s)
	{
		if (i == ‘(‘ || i == ‘[‘ || i == ‘{‘)
			push(stack, i);
		if (i == ‘)‘)
		{
			if (!isEmpty(stack))
			{
				if (isEmpty(stack) && Top(stack) != ‘(‘)
				{
					flag = false;
					break;
				}
				else
				{
					pop(stack);
				}
			}
			else
			{
				flag = false;
				break;
			}
		}
		else if (i == ‘]‘)
		{
			if (!isEmpty(stack))
			{
				if (isEmpty(stack) && Top(stack) != ‘[‘)
				{
					flag = false;
					break;
				}
				else
				{
					pop(stack);
				}
			}
			else
			{
				flag = false;
				break;
			}
		}
		else if (i == ‘}‘)
		{
			if (!isEmpty(stack))
			{
				if (Top(stack) != ‘{‘)
				{
					flag = false;
					break;
				}
				else
				{
					pop(stack);
				}
			}
			else
			{
				flag = false;
				break;
			}
		}
	}
	if (!isEmpty(stack))
		flag = false;
	else if (flag)
		cout << "MATCH" << endl;
	else
		cout << "ERROR"<<endl;

	delete stack;

	return 0;
}

  

以上是关于Balancing Symbols的主要内容,如果未能解决你的问题,请参考以下文章

多节点 cassandra 集群 - load_balancing_policy

poj1655Balancing Act

POJ 1655 Balancing Act (树的重心)

您应该将 .vscode/symbols 文件提交到源代码管理吗?

balancing loops 中文怎么翻译,如何区分与reinforcing loops

洛谷 P3138 [USACO16FEB]Load Balancing S(二维前缀和,离散化)