Garbled Email
Time limit60sMemory limit512 MB
Split the garbled string into dictionary words with changed letters spaced at least 5 apart while changing as few letters as possible.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Trie, String matching
- Solved
- No attempts yet
Problem
Gagan just got an email from her friend Jorge. The email carries important information, but it was corrupted in transit. All of the spaces are gone, and after the spaces were removed some of the letters were changed into other letters. All Gagan has now is a string of lowercase characters.
The original email was built out of words from the dictionary described below. The letters were changed after the spaces were removed, and the difference between the indices of any two changed letters is at least . For example, "code jam" could have become "codejam", "dodejbm", "zodejan", or "cidejab", but not "kodezam", because the distance between the index of the "k" change and the index of the "z" change is only .
What is the minimum number of letters that could have been changed?
The dictionary contains words of at least and at most lowercase characters and is given at the start of the input. It is not a dictionary from any natural language, though it does contain some English words. One input carries one dictionary, and every test case in that input uses it. The dictionary is given in lexicographically increasing order and contains no duplicate words.
Input
The first line of the input gives the number of words in the dictionary, . Each of the next lines contains a string of lowercase characters a to z representing one word of the dictionary. The next line gives the number of test cases, . test cases follow. Each test case is a single line containing a string of lowercase characters a to z.
Output
For each test case, print one line containing "Case #x: y", where x is the test case number starting from and y is the minimum number of letters that could have been changed while was produced.
Constraints
- Each word of the dictionary has at least and at most lowercase characters.
- The dictionary is sorted in lexicographically increasing order.
- The dictionary contains no duplicate words.
- The total number of characters in the dictionary is at most .
- can be produced by the method described above.
Note
The dictionary is not a natural language dictionary, so an English word is not guaranteed to appear in it. It may hold "operation" and "oo" while it does not hold "cooperation". A stretch that looks like "cooperation" then still has to be split into words the dictionary does hold.