The Killer Word (Small)
InterviewTime limit5sMemory limit512 MB
Pick the dictionary word that makes Sean miss the most guesses under his order-based guessing rule, breaking ties by dictionary order.
- Level
Medium5 of 10
- Topics
- Simulation, String, Brute force
- Solved
- No attempts yet
Problem
You are playing Hangman against your friend Sean. Sean is not good at this game. Use his imperfect strategy to make him lose as many points as possible.
+--+
| O
| / | \ Mystery word: _ a _ a _ a _
| / \
|
+-+---+
Hangman is played as follows.
- There is a dictionary D of all valid words, and both you and Sean know it. A word consists only of the characters
atoz, so there are no spaces. - You begin by choosing any word from D and writing it on a blackboard with every letter replaced by a blank
_. - On his turn, Sean chooses a letter and asks whether it is in the word. If it is, you reveal all positions of that letter. If it is not, Sean loses one point.
- Once every letter of the word has been revealed, the round ends.
- The round never ends early, no matter how many points Sean loses.
Sean uses a very simple strategy. He makes a list L of the 26 letters in some order and walks through it one letter at a time. If D contains at least one word that (a) has the letter he is thinking of and (b) agrees with what you have written down so far and with the results of all of Sean's previous guesses, then Sean guesses that letter. Otherwise he skips it. Either way, Sean then moves on to the next letter of the list.
Given Sean's list, which word should you choose to make Sean lose as many points as possible? If several choices cost him the same number of points, choose the one that appears first in D.
How the rules apply
Suppose Sean decides to guess the letters in alphabetical order (that is, L = abcdefghijklmnopqrstuvwxyz) and D contains the words banana, caravan and pajamas. If you choose pajamas, the round plays out like this.
- You begin by writing 7 blanks
_ _ _ _ _ _ _on the blackboard. From the number of blanks Sean knows at once that the word is eithercaravanorpajamas. - Sean guesses
abecause it comes first in L, and you reveal every position ofa:_ a _ a _ a _. - Sean skips
b, even thoughbananauses it. He already knows thatbananais not your word. - He then guesses
cbecause it appears incaravan. It does not appear in the word you actually chose, so Sean loses a point and nothing more is revealed. - Only
pajamasis left, so Sean guessesj,m,pandsin that order without losing any more points.
Choosing pajamas costs Sean one point. Either of the other two words would have cost him nothing.
Input
The first line of the input gives the number of test cases T. T test cases follow. Each test case begins with a line containing the integers N and M, the number of words in the dictionary and the number of lists to consider.
The next N lines contain the words of the dictionary, one per line: . Each word is an arbitrary sequence of the characters a to z.
The final M lines contain all of the lists Sean will use, one per line: . Each list is exactly 26 letters long and contains each letter exactly once. Sean uses these lists to guess letters as described above.
Limits
- Every word in D has between 1 and 10 characters.
- No two words in D are the same within a single test case.
Output
For each test case, output one line in the form Case #x: w1 w2 ... wM, where x is the test case number (starting from 1) and is the word you should choose when Sean guesses the letters in the order . If several words cost Sean the same number of points, output the one that appears first in the dictionary.