L-I-N-G-O
Time limit1sMemory limit128 MB
Given a secret five-letter word and guesses, print one feedback line per guess using upper-case for exact matches, lower-case for misplaced letters, and periods otherwise.
- Level
Medium4 of 10
- Topics
- String, Hash map, Simulation, Implementation
- Solved
- No attempts yet
Problem
A new TV game show challenges contestants to deduce a hidden five-letter word from the feedback they get by guessing other five-letter words.
The rules are:
- The show's staff pick a secret five-letter word.
- The contestant's goal is to guess that word.
- The first letter of the secret word is revealed at the start.
- The contestant then guesses a five-letter word. A computer reports, letter by letter, how close the guess is:
- a letter that is correct and in the correct position,
- a letter that appears in the secret word but is in the wrong position,
- a letter that does not appear in the secret word.
The feedback is printed as a single five-character line using this convention:
- an upper-case letter means the letter is correct and in the correct position;
- a lower-case letter means the letter is in the secret word but in the wrong position;
- a period (
.) means the letter is not in the secret word.
For example, suppose the secret word is HELLO. The contestant is told it begins with H, and guesses HOLES. The computer reports:
HoLe.
Here H and L are correct and in the correct position, o and e are in the word but misplaced, and S is not in the word at all. If a guess matches the secret word exactly, the report is simply the five upper-case letters of the secret word.
When the same letter appears more than once, each occurrence in the secret word can be credited to at most one letter of the guess: exact-position matches are marked first, and each remaining letter of the guess is marked lower-case only while an unused copy of it is still available in the secret word.
Write a program that evaluates the contestant's guesses and prints the one-line report for each guess.
Input
The input contains one or more games. Games are separated by a single blank line. Within a game, the first line is the secret word and the remaining lines are the contestant's guesses (there may be more or fewer guesses than needed).
- The secret word is always exactly five upper-case letters.
- Each guess must be validated: a valid guess is exactly five upper-case letters. Any other line (wrong length, lower-case letters, or other characters) is an invalid guess.
Input ends at the game whose secret word is LINGO: processing stops there, that game is not played, and nothing after it is read.
Output
Print the results of the games in order. Separate consecutive games' output with a single blank line (there is no blank line before the first game, and the terminating LINGO game produces no output).
For each game:
- First print the initial status report: the first letter of the secret word followed by four periods (for example,
H....). - Then, for each guess, print its one-line report. For an invalid guess, repeat the previous report unchanged. Every guess — valid or invalid — counts as one attempt.
- If a guess matches the secret word exactly, print that report and the game ends immediately (any further guesses are ignored).
- The contestant may make at most five guesses. If the secret word has not been found after the fifth guess — or if the game's guesses run out earlier — print the secret word in lower-case letters and move on to the next game.