#P13366. [GCJ 2011 #1A] The Killer Word

    ID: 15233 远端评测题 3000~6000ms 1024MiB 尝试: 0 已通过: 0 难度: 4 上传者: 标签>模拟2011递归记忆化搜索Google Code Jam

[GCJ 2011 #1A] The Killer Word

题目描述

You are playing Hangman with your friend Sean. And while you have heard that Sean is very good at taking candy from a baby, he is not as good at this game. Can you take advantage of Sean's imperfect strategy, and make him lose as badly as possible?

 +--+
 |  O
 | /|\       Mystery word: _ a _ a _ a _
 | / \
 |
+-+---+

Hangman is played as follows:

  • There is a dictionary DD of all valid words, which both you and Sean know. A word consists only of the characters a - z. In particular, there are no spaces.
  • You begin by choosing any word from DD, and writing it down on a blackboard with each letter replaced by a blank: _.
  • On his turn, Sean can choose any letter and ask you if it is in the word. If it is, you reveal all locations of that letter. Otherwise, Sean loses a point.
  • Once all letters in the word have 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 LL of the 26 letters in some order, and goes through the list one letter at a time. If there is at least one word in DD that (a) has the letter he is thinking of, and (b) is consistent with what you have written down so far and the result of all of Sean's previous guesses, then Sean guesses that letter. Otherwise, he skips it. No matter what, Sean then moves on to the next letter in his list.

Given Sean's list, what word should you choose to make Sean lose as many as points as possible? If several choices are equally good, you should choose the one that appears first in DD.

Example

Suppose Sean decides to guess the letters in alphabetical order (i.e., L=L = "abcdefghijklmnopqrstuvwxyz"), and DD contains the words banana, caravan, and pajamas. If you choose pajamas, the game would play out as follows:

  • You begin by writing 7 blanks _ _ _ _ _ _ _ on the blackboard. Based on the number of blanks, Sean knows immediately that the word is either caravan or pajamas.
  • Sean begins by guessing a since it is first in LL, and you reveal all locations of the letter a on the blackboard: _ a _ a _ a _.
  • Sean skips b even though it is used in banana. Sean already knows that is not your word.
  • He then guesses c because it appears in caravan. It does not appear in the word you actually chose though, so Sean loses a point and nothing more is revealed.
  • By process of elimination, Sean now knows your word has to be pajamas, so he proceeds to guess j, m, p, and s in order, without losing any more points.

So Sean loses one point if you choose pajamas. He would have gotten either of the other words without losing any points.

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case begins with a line containing integers NN and MM, representing the number of words in the dictionary and the number of lists to consider.

The next NN lines contain the words in the dictionary, one per line: D1D_1, D2D_2, ..., DND_N. Each word is an arbitrary sequence of characters a - z.

The final MM lines contain all of the lists Sean will use, one per line: L1L_1, L2L_2, ..., LML_M. Each list is exactly 26 letters long, containing each letter exactly once. Sean will use these lists to guess letters as described above.

输出格式

For each test case, output one line containing "Case #x: w1w_1 w2w_2 ... wMw_M", where xx is the case number (starting from 1) and wiw_i is the word you should choose if Sean guesses the letters in order LiL_i. If multiple words cause Sean to lose the same number of points, you should choose the option that appears first in the dictionary.

2
3 2
banana
caravan
pajamas
abcdefghijklmnopqrstuvwxyz
etaoisnhrdlcumwfgypbvkjxqz
4 1
potato
tomato
garlic
pepper
zyxwvutsrqponmlkjihgfedcba
Case #1: pajamas caravan
Case #2: garlic

提示

Limits

  • 1T101 \leq T \leq 10.
  • Each word in DD will have between 11 and 1010 characters inclusive.
  • No two words in DD will be the same within a single test case.

Small dataset (10 Pts, Test set 1 - Visible)

  • 1N1001 \leq N \leq 100.
  • 1M101 \leq M \leq 10.
  • Time limit: 30 3 seconds.

Large dataset (20 Pts, Test set 2 - Hidden)

  • 1N100001 \leq N \leq 10000.
  • 1M1001 \leq M \leq 100.
  • Time limit: 60 6 seconds.