HomeVolumesContestsSectionsForumsUsersPrintHelpAbout

Sections > Linear Data structures: Stacks, Queues, Linked Lists, etc > problem:


50775 - Balanced Parenthesis

Guest
• Discussion of problem (1)

Section problems

• 50262 - Brackets
• 50291 - Postfix Arithmetic Expressions
• 50286 - Remove Duplicates
• 50775 - Balanced Parenthesis
• 50340 - Game 19
• 50877 - Friendly Queue
• 51086 - Top popular student
• 50774 - Hot Potato
• 50777 - Dwarfs Maze
• 50781 - ReversesreveR
• 50780 - Hot Potato - Revisited
• 50564 - Mother's Milk Buckets

Feedback

If you notice incorrect translations in Contester, please let author know.

Time limit 2000/4000/4000/4000 ms. Memory limit 65000/65000/65000/65000 Kb.
Prepared by Ibrahim Mesecan. Difficulty Beta

Balanced Parenthesis

An application of a stacks is to determine whether the parentheses in an expression are balanced. For example, the expression
  (a + [c / d] )
  1    2     2 1

has balanced parentheses. We can solve this problem without using a stack by ignoring all characters except the symbols (, ), [, ], {, and }.

For example, the expression
  (a + b * {c / [d - e]}) + (d / e)
is balanced, but the expression
  (a + b * {c / [d - e } ) + (d / e)
is not because the subexpression [d - e} is incorrect.

Question: Write a program that is going to read the an arithmetic expression, and decide if it's balanced or not.
Note: Use Stacks to solve the problem.

Input specification
You will be given an expression which might include numbers arithmetic operators and the set of parenthesis symbols: (, ), [, ], {, and }. The expression will be in one line and containing at most 100 chars including the spaces.

Output specification
If the expression is balanced show "Balanced", otherwise show "Not Balanced" on screen.

Sample Input I
  (a + b * {c - e } ) + ( d / e   
Sample Input II
  (a * {c - e } )
Sample Output I
  Not Balanced
Sample Output II
  Balanced

Для отправки решений необходимо выполнить вход.

www.contester.ru