Problem 443
Longest String Chain
Medium · 20 pts · Array, String, Dynamic Programming, Hash Table
Problem
Word A is a predecessor of word B if you can insert exactly one letter anywhere in A, without changing the order of the other letters, to get B. For example, 'abc' is a predecessor of 'abac'. A word chain is a sequence of words where each word is a predecessor of the next. Using words from the given list, find the length of the longest possible chain. A single word is a chain of length 1. Sort the words by length and, for each word, try deleting each letter to find its possible predecessors.
Input
First line: N. Second line: N distinct words of lowercase letters.
Output
Print the length of the longest word chain.
Constraints
1 <= N <= 1000; 1 <= word length <= 16
Example input
6 a b ba bca bda bdca
Example output
4
Explanation
From a b ba bca bda bdca, one longest chain is a → ba → bda → bdca, adding one letter each time, so the answer is 4.