Diligent Beavers
InterviewTime limit2sMemory limit1024 MB
Given ten rules for a beaver moving along an infinite row of trees, decide whether the beaver can become happy starting from the angry mood.
Statement
Beavers are unique animals. They are best known for building dams across rivers.
Let us look at the dam building process in detail. At the start, a beaver stands in front of a long row of trees. The row is infinite in both directions. Each tree is either good or bad. Initially all trees are good. When the beaver gnaws on a good tree, it may become bad, and when it gnaws on a bad tree, it may become good.
The beaver can be in one of five moods: angry, busy, creative, despaired, or exhausted. Initially the beaver is angry.
When the beaver stands in front of a tree, it does the following depending on its mood and whether the tree in front of it is good:
- gnaws the tree, which may change it from bad to good or from good to bad;
- changes its mood to one of the five possible moods (the mood may stay the same);
- moves either to the neighboring tree on the left or to the neighboring tree on the right.
For some combinations of mood and tree state, the beaver may decide that the dam is finished and become happy. It may also happen that the beaver never becomes happy.
You are given the rules the beaver follows. Determine whether this beaver becomes happy.
Input
The input file contains ten words separated by spaces. Each word has three characters and defines the beaver's behavior. The first word defines the behavior of an angry beaver in front of a good tree, the second word defines an angry beaver in front of a bad tree, the third word defines a busy beaver in front of a good tree, and so on, with the last word defining an exhausted beaver in front of a bad tree.
The first character is 1 if the beaver makes the tree bad, or 0 if it makes the tree good. The second character is R if the beaver moves right, or L if it moves left. The third character is a letter from A to E giving the new mood, or H if the beaver becomes happy.
Output
Print happy beaver if the beaver becomes happy, or unhappy beaver if it does not.