Similar Words
Time limit4sMemory limit512 MB
Given a set of distinct words, choose as many prefixes as possible so that no two chosen words differ by deleting one leading letter.
- Level
Hard8 of 10
- Topics
- Trie, Tree, Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
A non-empty sequence of lowercase English letters is called a word. A prefix of a word is a word that can be obtained from by removing zero or more of its last letters.
Two words are called similar if one of them can be obtained from the other by removing its first letter.
You are given a set of words. Find the maximal possible size of a set of non-empty words such that:
- every word in is a prefix of some word in ;
- contains no two similar words.
Input
The input contains multiple test cases. The first line contains an integer , the number of test cases. The descriptions of the test cases follow.
The first line of each description contains an integer , the number of words in the set (). Each of the following lines contains one non-empty word, an element of . All words in are distinct.
The total length of all words in one input does not exceed .
Output
For each test case, print one line containing one integer , the maximal number of words that can contain.