IOI'95 Task: Word Chains

[ IOI Home page ] Task: Word Chains

A word consists of at least one and at most 75 lowercase letters (from `a' to `z'). A list of one or more words is called a chain when each word in that list, except the first, is obtained from the preceding word by appending one or more letters on the right. For instance, the list

is a chain of four words, but the list
is not a chain. Note that every list of one word is a chain.

Input Data

Your program is given a list of words in the file INPUT.TXT. This list contains at least one word and all of the words together have no more than 2,000,000 letters. The input is terminated with a line containing a single period (`.'). All other lines contain one word each. The list is sorted lexicographically using the standard alphabetical ordering (as in an English dictionary). There are no duplicate words in the list.

Output Data

The length of a chain is the number of words it contains. Your program should write to file OUTPUT.TXT a longest chain of words taken from the input. Each word should be on a separate line.

If there is more than one chain of maximum length, your program should output only one of these chains. It does not matter which one.

Example Input and Output

Figure 1 gives an input file with seven words and the corresponding output file. The input contains one chain of length four and no chains with more than four words.

____________    _____________
|__________|    |___________|
|i         |    |i          |
|if        |    |in         |
|in        |    |int        |
|input     |    |integer    |
|int       |    |___________|
|integer   |                 
|output    |                 
|.         |                 
Figure 1: Example input and output

IOI 95