Word Game
InterviewTime limit1sMemory limit1024 MB
Decide whether all dictionary words can form one chain where each word starts with the previous word's last character, and print an order if so.
Problem
Children play a word chain game. Each player must say a word that begins with the same character that the previous word ends with.
You are given a dictionary of words. Determine whether the children can play so that every word in the dictionary is used exactly once. All words in the dictionary are distinct. Words consist of the characters A to Z, a to z, and 0 to 9, and uppercase and lowercase letters are different characters.
Input
The first line contains the number of words in the dictionary (). Each of the next lines contains one word. No word is longer than 10 characters.
Output
If all words can be ordered according to the rules, print JAH on the first line, followed by exactly lines that give one possible order of the words, where each word begins with the same character that the previous word ended with. Otherwise, print EI on a single line.