Almost Prefix-Free Codes
Time limit2sMemory limit1024 MB
Choose the largest subset of given words such that the longest common prefix of any two chosen words has length at most k.
- Level
Hard8 of 10
- Topics
- Trie, Tree, Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
In coding theory, prefix-free codes are used often: sets of words such that no word is a prefix of another. (A word is a prefix of a word if can be obtained from by deleting zero or more characters from its end. For example, the words <<>>, <>, <>, and <> are prefixes of the word <>.) For instance, the set of words <>, <>, and <> is a prefix-free code, while the set <>, <>, <> is not, because <> is a prefix of <>.
Professor Deshifro works at the laboratory for the study of useless information and is researching his new invention: almost prefix-free codes. A set of words is called an almost prefix-free code of level if the longest common prefix of any two words in the set has length at most . For example, the set <>, <>, <> is an almost prefix-free code of level 2, while the set <>, <>, <> is not, because the longest common prefix of <> and <> has length 3.
The next task Professor Deshifro has assigned to his assistants is the following: given a set of words and a number , choose from the given words a maximum set that is an almost prefix-free code of level . As the most junior assistant, you have been tasked with writing the corresponding program.
Input
The first line of the input file contains two integers and : the number of words in the given set and the level of the almost prefix-free code to be built (, ). The next lines contain one word each. The words consist of lowercase Latin letters. The length of each word is from 1 to 200 characters. The total length of all words does not exceed . All words are distinct.
Output
On the first line of the output file, print a single number : the maximum number of words that can be chosen from the given set so that they form an almost prefix-free code of level . The next lines must contain the chosen words.