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 – 2015-01-30T10:04:18.680
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 – 2015-01-30T10:15:49.820
@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 – 2015-01-30T10:50:19.387
FUZxxi, How about "Growing Words"? – DavidC – 2015-01-30T15:27:34.333
@DavidCarraher Better this way? – FUZxxl – 2015-01-30T17:23:18.947
Yes, I think it is better. Calls attention nicely. – DavidC – 2015-01-30T18:27:02.090
Is the input supposed to be the actual words, the name of a file containing the words, or something else? – KSFT – 2015-01-31T05:05:07.733
@KSFT The input is a newline separated list of words. – FUZxxl – 2015-02-01T22:39:03.353