Prefix
InterviewTime limit2sMemory limit128 MB
Given up to 50 words, find the largest subset where no word is a prefix of another, using a trie and tree DP.
- Level
Medium5 of 10
- Topics
- Trie, Dynamic programming, Tree, String
- Solved
- No attempts yet
Problem
A set of words is prefix-free if no word in the set is a prefix of another word in the same set. For example, {hello}, {hello, goodbye, giant, hi}, and the empty set are all prefix-free. However, {hello, hell} and {giant, gig, g} are not prefix-free.
Given a collection of N words, find the maximum possible size of a subset that is prefix-free.
Input
The first line contains the number of words N. N is a positive integer not greater than 50.
Each of the next N lines contains one word. Every word consists only of lowercase English letters and has length at most 50. The same word may appear more than once in the given collection.
Output
Print the answer on the first line.