Brackets
Time limit1sMemory limit128 MB
For each line of code until the '#' line, decide whether its round, square, and curly brackets are matched and correctly nested.
Problem
If you have programmed in C or Java, you are used to dealing with brackets. In this problem we consider three kinds of brackets: round (), square [], and curly {}. Every opening bracket must have a matching closing bracket, and the brackets must be correctly nested.
You are given several pieces of code. For each piece, decide whether its brackets are legal or not. To keep things simple, assume that no bracket is enclosed in quotes. Newline characters have also been removed, so each line of input represents one piece of code to be checked.
Input
The input consists of several lines of text (code), each containing at most 250 characters. The last line of input contains only a single #, and this line must be ignored. For every line before the # line, check whether its brackets are legal and report the result.
Output
For each piece of code, if it contains no brackets, or if all brackets are correctly paired and nested, print Legal on its own line. If there is any error, print Illegal on its own line.