eolymp
bolt
Try our new interface for solving problems
Problems

Cow Cotillion

Cow Cotillion

The cow cotillion, a fancy dance every spring, requires the cows (shown as ">") and bulls (shown as "<") to bow to each other during a dance. Schematically, one properly bowing pair of cattle is shown like this: "> <". Sometimes another pair of cattle will sashay between a pair of bowing cows: "> > < <". In fact, sometimes a larger number of cows will mix it up on the dance floor: "> > < < > <" (this includes a second set of bowing cows on the right). Complex arrangements can be perfectly legal dance formations: \includegraphics{https://static.eolymp.com/content/6c/6c28f907d30704f203a66289f6c73749bd534cea.gif} Farmer John notices that a stray heifer sometimes sneaks into a group and unbalances it: "> > < < < <". This is strictly forbidden; FJ wants to punish the interlopers. FJ has copied down records of as many as $500$ cows participating in dance lines and wonders if the dance line is properly balanced (i.e., all of the cattle can be paired off in at least one way as properly bowing pair by pair). He copied only the direction each cow was bowing without any extra spaces to help determine which cow was bowing to which bull, strings like this rendition of the illegal example from the previous paragraph: "> > < < < <". He wants you to write a program to tell him if the dance line is legal. FJ has $n$ pattern recordings $p_i$ comprising just the characters '>' and '<' with varying length $k_i~(1 \le k_i \le 200)$. Print "\textbf{legal}" for those patterns that include proper pairs of bowing cows and "\textbf{illegal}" for those that don't. \InputFile First line contains single number $n~(1 \le n \le 1000)$. Each of the next $n$ lines contains an integer followed by a space and a string of $k$ characters '>' and '<': $k_i$ and $p_i$. \OutputFile Each line must contain either the word "\textbf{legal}" or "\textbf{illegal}" depending on whether the corresponding input has a legal bowing configuration.
Time limit 1 second
Memory limit 128 MiB
Input example #1
2
6 >><<><
4 ><<>
Output example #1
legal
illegal
Source 2011 USACO February, Bronze