[Data Structure] An Algorithm for Matching Delimiters

An important task when processing arithmetic expressions is to mach delimiters.

We can use Stack to solve this problem.

def is_matched(expr):
	left=‘({[‘
	right=‘)}]‘

	S=ArrayStack()

	for c in expr:
		if c in left:
			S.push(c)
		elif c in right:
			if S.is_empty():
				return False
			if right.index(c)!=left.index(S.pop()):
				return False
	return S.is_empty()

  

原文地址:https://www.cnblogs.com/chiyeung/p/9661445.html

时间: 2024-11-13 10:16:08

[Data Structure] An Algorithm for Matching Delimiters的相关文章

资源向导之 "Data structure and Algorithm"

几本神书: 一.最佳入门--DSAA 这门书相关的代码. http://users.cis.fiu.edu/~weiss/dsaa_c2e/files.html 我自己也写了一些. 二.大名鼎鼎的CLRS. MIT Introduction to algorithm http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/in

《Data Structure and Algorithm Analysis in Java》第 3 章 - 表

1.抽象数据类型 抽象数据类型(abstract data type,ADT)是带有一组操作的一些对象的集合.在 ADT 的定义中没有地方提到关于这组操作是如何实现的任何解释. Java 类也考虑到 ADT 的实现,不过适当地隐藏了实现的细节.如果由于某种原因需要改变实现的细节,通过仅仅改变执行这些 ADT 操作的例程应该是很容易做到的.这种改变对于程序的其余部分是完全透明的. 对以每种 ADT 并不存在什么法则来告诉我们必须要有哪些操作,这是一个设计决策.错误处理和结构调整(在适当的地方)一般

《Data Structure and Algorithm Analysis in Java》中Java语言的重要特点 - 实现泛型构件 pre-Java 5

面向对象的一个重要目标就是对代码重用的支持.支持这个目标的一个重要机制就是泛型机制(generic mechanism):如果出去对象的基本类型外,实现方法是相同的,就可以用泛型实现(generic implementation)来描述这种基本功能.在Java 1.5版本以前,Java并不直接支持泛型实现,泛型编程的实现是通过使用继承的一些基本概念来完成的. 1. 使用Object表示泛型 Java中的基本思想就是通过使用像Object这样适当的超类来实现泛型. public class DSA

Use the Right Algorithm and Data Structure

Use the Right Algorithm and Data Structure Jan Christiaan "JC" van Winkel A big bank with many branch offices complained that the new computers it had bought for the tellers were too slow. This was in the time before everyone used electronic ban

uva 11995 - I Can Guess the Data Structure!

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=3146&mosmsg=Submission+received+with+ID+14262472 I Can Guess the Data Structure! There is a bag-like data structure, supporti

hdu-5929 Basic Data Structure(双端队列+模拟)

题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 207    Accepted Submission(s): 41 Problem Description Mr. Frog learned a basic data structure recently, which is called

HDU 4217 Data Structure?(线段树 or 树状数组啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4217 Problem Description Data structure is one of the basic skills for Computer Science students, which is a particular way of storing and organizing data in a computer so that it can be used efficiently

HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)

题目大概说给一张无向点带有权无向图.定义连通图的权值为图中各点权的乘积,图的权值为其包含的各连通图的权和.设$z_i$为删除i点后图的权值,求$S = (\sum\limits_{i=1}^{n}i\cdot z_i) \text{ mod } (10^9 + 7)$. 官方题解这么说的: 显然, 只要删掉关键点才会使图不联通. 对于其他点, 权值很容易计算. 首先求出所有的点双联通分量, 对于每一个点双联通分量$S$, 新建一个节点$s$, 向$S$中每个节点$v$连边. 这样一来, 新增的点

CDOJ 483 Data Structure Problem DFS

32‘20 Data Structure Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/483 Description Data structure is a fundamental course of Computer Science, so that each contestant is highly likely to solve this data s