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;
}

  

时间: 2024-10-18 15:03:27

Balancing Symbols的相关文章

poj1655 Balancing Act 求树的重心

http://poj.org/problem?id=1655 Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9072   Accepted: 3765 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a fo

re正则表达式13_review of regex symbols

Review of Regex Symbols This chapter covered a lot of notation, so here’s a quick review of what you learned: The ? matches zero or one of the preceding group. The * matches zero or more of the preceding group. The + matches one or more of the preced

【iOS】duplicate symbols for architecture x86_64

今天遇到了这个问题,错误如下: duplicate symbol _OBJC_IVAR_$_BCViewController.bank in: /Users/***/Library/Developer/Xcode/DerivedData/***-fmtpkcbvfajuuadtvwtzfyjxoywz/Build/Intermediates/***.build/Debug-iphonesimulator/***.build/Objects-normal/x86_64/BCViewControll

Load Balancing 折半枚举大法好啊

Load Balancing 给出每个学生的学分.   将学生按学分分成四组,使得sigma (sumi-n/4)最小.         算法:   折半枚举 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <

ios build时,Undefined symbols for architecture xxx问题的总结

这2天升级到xcode6,用ios8 SDK编译老项目,各种Undefined symbols for architecture xxx,精神差点崩溃了.不过最后还是解决了,本文简单总结一下 简单来说,Undefined symbols基本上等于JAVA的ClassNotFoundException,最常见的原因有这几种: build的时候没有加framework 比如说,有一段代码我用了OpenGL,引入了头文件 #import <OpenGLES/ES2/glext.h> build的时候

iOS 创建静态库文件时去掉其中的Symbols

在工程中创建静态库文件时,默认会将一些Symbols加到静态库文件中,这样做有两个缺点: 1.如果引用静态库文件的工程中发生了bug,就会直接跳转到静态库的源码. 2.静态库文件的大小会因此翻几番.本人最近做的这个静态库文件中,去掉symbols前大小为7.8MB左右,去掉以后大小为2.8MB. 要去掉Symbols,首先打开Build Settings并选中静态库的Target,然后设置下列选项: 如果有错误或遗漏,欢迎批评指正. iOS 创建静态库文件时去掉其中的Symbols,布布扣,bu

oc调用c++接口时 报错 Undefined symbols for architecture i386:

当在oc中调用c++中的方法时,发现说c++中的方法没定义或是找不到 Undefined symbols for architecture i386: "_desTYData", referenced from:-[TuYoo encryptParametersWithDict:] in libtuyoo.a(TuYoo.o)ld: symbol(s) not found fo 记得c++中的方法定义是要这样定义的 extern"C" { const char *d

POJ1655 Balancing Act(树的重心)

题目链接 Balancing Act 就是求一棵树的重心,然后统计答案. 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define REP(i,n) for(int i(0); i < (n); ++i) 6 #define for_edge(i,x) for(int i = H[x]; i; i = X[i]) 7 8 const int INF = 1 << 30; 9 const int N = 10

How Node.js Multiprocess Load Balancing Works

As of version 0.6.0 of node, load multiple process load balancing is available for node. The concept of forking and child processes isn't new to me. Yet, it wasn't obvious to me how this was implemented at first. It's quite easy to use however: var c