6
An incremental word chain is a sequence of words of a vocabulary such that each word is the result of either prepending or appending a single character to the previous word, ignoring capitalization. An example of such a chain can be found below.
I → is → his → hiss
Your task is to write a program that for a given vocabulary finds the longest incremental word chain. The input is provided from standard input a vocabulary as one word per line. The words are composed of the lowercase letters a to z only. The output of your program shall go to standard output and shall be one word per line a longest word chain of the input vocabulary, starting with the shortest word.
This is code golf, the solution with the least character count wins. Different forms of input or output are not possible, but the terms standard input and standard output may be interpreted in a reasonable way for the language you are solving this problem in.
Please demonstrate that your code works by publishing the longest word chain you find on the xz-compressed vocabulary found here. This vocabulary was generated by executing the following series of commands on the file british-english-insane from the SCOWL project in the C locale:
tr 'A-Z' 'a-z' <british-english-insane | sed -n '/^[a-z]*$/p' | sort -u | xz -9e >voc.xz
A solution in polynomial time is possible and highly appreciated.
@MartinBüttner I agree with you that these challenges are similar, but an important difference is that in my challenge the graph is acyclic, so a solution in polynomial time can be found. – FUZxxl – 11 years ago
Title and description don't match, as the title allows only appending whereas the description also allows prepending. Description trumps title, doesn't it? – nimi – 11 years ago
@nimi You are correct. To put it simply, I didn't find a formulation of the problem that fits in the title and is correct. Maybe you can help me find one? – FUZxxl – 11 years ago
FUZxxi, How about "Growing Words"? – DavidC – 11 years ago
@DavidCarraher Better this way? – FUZxxl – 11 years ago
Yes, I think it is better. Calls attention nicely. – DavidC – 11 years ago
Is the input supposed to be the actual words, the name of a file containing the words, or something else? – KSFT – 11 years ago
@KSFT The input is a newline separated list of words. – FUZxxl – 11 years ago