#P3633. Cheating Hangman
Cheating Hangman
Cheating Hangman
This problem is based on a variant of the traditional hangman game called "Cheating Hangman". In this two‐player game, player A secretly picks a word from a known corpus and only shows a series of blank spaces (one per letter) without writing down the word. Player B then guesses letters one by one. For each guessed letter, player A responds as follows:
- If the guessed letter appears in the secret word, A writes the letter in the corresponding positions. (If all positions are filled the game ends with B winning.)
- If the guessed letter does not appear in the word, A writes the letter below one of a series of blank spaces (there are exactly as many error slots as the number of letters in the word). If after a guess all error slots are filled, then B loses and A wins.
However, A is allowed to cheat. Although initially A mentally chooses a word length and considers only words from the corpus with that length, he does not commit to a specific word. After each guess from B, A is free to change his secret word as long as his previous answers remain valid with respect to the new candidate word. In particular, A can always answer a guess as a miss (i.e. claim that the letter is not in the word) if there is at least one candidate word that does not contain the guessed letter.
Thus A can follow the following cheating strategy. Suppose B has guessed a set X of letters incorrectly (with |X| < n, where n is the word length chosen by A). Let the current candidate set be
$$W = \{ w \text{ (of length } n\text{) } : w \cap X = \emptyset \}. $$If for every possible subset X of letters with |X| < n
we have W \neq \emptyset
, then A can always answer with a miss until B makes n
wrong guesses and loses. On the other hand, if there exists some set X with |X| < n
such that W
is empty (i.e. every word of that length contains at least one letter from X), then B can force a situation in which A must reveal a letter, eventually allowing B to win.
In other words, let \( W_n \) be the set of words from the corpus of length \( n \). Define the hitting number \( h(W_n) \) as the minimum size of a set \( X \subseteq \{A,\ldots,Z\} \) such that every word in \( W_n \) contains at least one letter from \( X \). Then A can force a win with word length \( n \) if and only if \( h(W_n) \ge n \). (All formulas are in \( \LaTeX \) format.)
Your task is: Given a corpus of words, determine whether there exists a word length \( n \) for which player A has a winning (cheating) strategy, i.e. \( h(W_n) \ge n \). If such an \( n \) exists, print YES
; otherwise, print NO
.
Note: At the end of the game if A wins, he must be able to announce a word from the corpus (of the chosen length) which is consistent with all his previous responses.
inputFormat
The first line contains an integer m
(1 ≤ m ≤ 100), the number of words in the corpus. Each of the following m
lines contains a non-empty uppercase string (only letters A-Z) representing a word.
You may assume that the length of each word is at most 15.
outputFormat
Output a single line with YES
if there exists a word length n
for which A can guarantee a win using the cheating strategy described above; otherwise, output NO
.
sample
4
AB
AC
AD
AE
NO