#P8079. [WC2022] 猜词

[WC2022] 猜词

Background

Abusing the judging system for this problem will result in an immediate account ban.

Due to limitations of the judge environment, do not submit this problem using the C++14 (GCC 9) language, otherwise it may cause compilation errors.

The official testdata sent by the problem setter is used, but due to disputes, the “community testdata” name is temporarily kept.

60 s, 1 GiB, -O2, interactive.

When submitting on Luogu, please note the following two points:

  • Please remove the word.h header file from your code.

  • Do not use rand() for randomness. Please replace it with mt19937. Be careful not to reuse a conflicting name, otherwise you will get CE. For details, see here.

This is an interactive problem.

Problem Description

In this problem, you need to play a classic game with the interactive library. In each game, the interactive library will generate a 55-letter word from the dictionary and tell you its first letter. You need to guess it within 55 attempts.

Each guess must be a word that exists in the dictionary. If you guess correctly, the game ends. After each incorrect guess, the interactive library will return which letter positions are correct (shown as gold) and which letters appear in the target word but are in the wrong position (shown as silver).

Specifically, the interactive library returns two boolean arrays gold\texttt{gold} and silver\texttt{silver}. gold[i] (0i<50 \leq i < 5, same below) indicates whether the ii-th letter is guessed correctly (both position and character are correct). silver[i] indicates, if the ii-th letter is not correct (i.e., not gold), whether this letter has appeared in the non-gold part of the target word.

For example, if the target word is panic\texttt{panic} and you guess paper\texttt{paper}, the interactive library returns gold[0] = true ( p\texttt{p} is correct), gold[1] = true ( a\texttt{a} is correct), and all others are false (note that although the second p\texttt{p} in paper\texttt{paper} appears in panic\texttt{panic}, it appears in the gold part of this guess, so silver[2] = false).

As another example, if the target word is apple\texttt{apple} and you guess paper\texttt{paper}, the interactive library returns gold[2] = true ( p\texttt{p} is correct), silver[0] = true ( p\texttt{p} appears in the non-gold part of the target word), silver[1] = true ( a\texttt{a} appears), silver[3] = true ( e\texttt{e} appears), and all others are false.

Scoring

Because each game has a high degree of randomness, you need to play T=1000T = 1000 games in a row. The scoring for each game is as follows:

  • If any guessed word does not have length 55, or the guessed word is not in the dictionary, you get 00 points.
  • If you guess correctly on the 11-st attempt, you get 150150 points.
  • If you guess correctly on the 22-nd attempt, you get 120120 points.
  • If you guess correctly on the 33-rd attempt, you get 100100 points.
  • If you guess correctly on the 44-th attempt, you get 9090 points.
  • If you guess correctly on the 55-th attempt, you get 8585 points.
  • If all 55 attempts are wrong, you get 00 points.

Your score for this problem is the smaller one of the following two values: the average score over the 10001000 games, and 100100.

How to use the interactive library

This problem only supports C++.

You may only submit one source file word.cpp to implement the following functions, and you must follow the naming and interfaces below.

You need to include the header file word.h.

You do not need to, and should not, implement main.

The functions you need to implement are:

const char *guess(int num_testcase, int remaining_guesses, char initial_letter, bool *gold, bool *silver);
void init(int num_scramble, const char *scramble);

For the ii-th game, the parameter num_testcase is ii (1iT1 \leq i \leq T). In each game, the guess function will be called 151 \sim 5 times. For the jj-th call, the parameter remaining_guesses is 6j6-j (1j51 \leq j \leq 5). The parameter initial_letter is the first letter of the target word in the current game (guaranteed to be a lowercase letter). It is guaranteed that across calls to guess, num_testcase is non-decreasing. It is guaranteed that when num_testcase is the same, initial_letter does not change and remaining_guesses is strictly decreasing. If a guess is correct or illegal, that game ends, and the next call to guess will be for the next game.

The boolean arrays gold and silver are as described above. When remaining_guesses is 55, the gold and silver arrays are not available (i.e., they may be null pointers), so please avoid using them. When remaining_guesses is less than 55, gold and silver are two boolean arrays of size 55, storing the result of the previous guess.

The return value of guess must be a string of length 55, representing your guessed word. This word must be in the dictionary.

The init function will be called exactly once before all calls to guess. The parameter num_scramble is the dictionary size. scramble is a string of length num_scramble×5\texttt{num\_scramble} \times 5, storing all words in the dictionary. Each word has length 55, with no separators between words.

Additional files

The provided files are word.h, word_sample.cpp, play.cpp, grader.cpp, scramble.txt, scramble.csv, scramble_pure.txt.

word_sample.cpp is a sample implementation of word.cpp that you need to write.

grader.cpp is a sample judging program (compile command: g++ ‐o grader grader.cpp word.cpp).

scramble.txt, scramble.csv, scramble_pure.txt are the dictionary files used in this problem. In scramble.txt, words are separated by newline characters. In scramble.csv, words are separated by commas. In scramble_pure.txt, words are not separated (that is, it is the same as the scramble parameter in init).

play.cpp is a program that lets you play this game with your own program (compile command: g++ ‐o play play.cpp word.cpp). The input and output formats of play.cpp are described below.

Input Format

Sample Input Format

The first line contains a positive integer TT, representing the number of games.

For each of the next TT games, the first line contains a lowercase letter, representing the first letter of the target word.

Then there are 040 \sim 4 lines. Each line is a string of length 55 consisting of g\texttt{g}, s\texttt{s}, and -\texttt{-}. At position ii (0i<50 \leq i < 5, same below), g\texttt{g} means gold[i] = true, s\texttt{s} means silver[i] = true, and -\texttt{-} means gold[i] = silver[i] = false. If you guess correctly or the guessed word is illegal, the game ends, so the number of input lines is variable.

Output Format

Sample Output Format

For every input line except the first line containing the number of games, output a string of length 55, representing the guessed word. Extra blank lines are added in the sample output for readability.

7
p
gg---
gg---
ggg--
a
g----
ssgs-
a
g---g
gggg-
a
g---g
g---g
g---g
g---g
a
a
c
-sss-

paper
paths
panda
panic

aargh
paper
apple

afore
apply
apple

apple
apple
apple
apple
apple

abcde

apple

kraal
cobra

Hint

Sample Explanation

In game 11, the target word is panic\texttt{panic}, and it is guessed correctly on the 44-th attempt.

In game 22, the target word is apple\texttt{apple}, and it is guessed correctly on the 33-rd attempt.

In game 33, the target word is apple\texttt{apple}, and it is guessed correctly on the 33-rd attempt. Note that even if every position has been guessed as gold at least once, you still need an additional guess to count as guessing correctly.

In game 44, the target word is above\texttt{above}, and all 55 attempts are wrong. Note that the result of the 55-th guess does not need to be input, and it will not be passed into the guess function.

In game 55, the target word is apple\texttt{apple}. The 11-st guess is illegal (not in the dictionary), so the game ends immediately. Note that the sample program will not automatically detect this situation.

In game 66, the target word is apple\texttt{apple}, and it is guessed correctly on the 11-st attempt.

In game 77, the target word is cobra\texttt{cobra}. Note that when guessing kraal\texttt{kraal}, both a\texttt{a} are silver. Also note that since the sample program does not know what the target word is, you need to terminate the program manually.

Constraints

For 100%100\% of the data, T=1000T = 1000, num_scramble=8869\texttt{num\_scramble} = 8869. Each target word is generated independently and uniformly at random from the set of all words in the dictionary. These words are fully determined before any calls to guess, and will not be constructed dynamically based on the interaction process with your program. The interactive library itself uses no more than 11 second of time and no more than 16 MiB of memory.

Since this problem has only 11 set of testdata, runtime errors, time limit exceeded, memory limit exceeded, and similar errors will all cause the total score for this problem to be 00. It is recommended to check carefully to avoid such errors.

Translated by ChatGPT 5