CF753C Interactive Bulls and Cows (Hard)
Description
The only difference from the previous problem is the constraint on the number of requests. In this problem your program should guess the answer doing at most 7 requests.
This problem is a little bit unusual. Here you are to implement an interaction with a testing system. That means that you can make queries and get responses in the online mode. Please be sure to use the stream flushing operation after each query's output in order not to leave part of your output in some buffer. For example, in C++ you've got to use the fflush(stdout) function, in Java — call System.out.flush(), and in Pascal — flush(output).
Bulls and Cows (also known as Cows and Bulls or Pigs and Bulls or Bulls and Cleots) is an old code-breaking paper and pencil game for two players, predating the similar commercially marketed board game Mastermind.
On a sheet of paper, the first player thinks a secret string. This string consists only of digits and has the length $ 4 $ . The digits in the string must be all different, no two or more equal digits are allowed.
Then the second player tries to guess his opponent's string. For every guess the first player gives the number of matches. If the matching digits are on their right positions, they are "bulls", if on different positions, they are "cows". Thus a response is a pair of numbers — the number of "bulls" and the number of "cows". A try can contain equal digits.
More formally, let's the secret string is $ s $ and the second player are trying to guess it with a string $ x $ . The number of "bulls" is a number of such positions $ i $ ( $ 1
Input Format
To read answers to the queries, the program must use the standard input.
The program will receive pairs of non-negative integers in the input, one pair per line. The first number in a pair is a number of bulls and the second one is a number of cows of the string $ s $ and the string $ x_{i} $ printed by your program. If the system response equals "4 0", then your solution should terminate.
The testing system will let your program read the $ i $ -th pair of integers from the input only after your program displays the corresponding system query in the output: prints value $ x_{i} $ in a single line and executes operation flush.
Output Format
The program must use the standard output to print queries.
Your program must output requests — $ 4 $ -digit strings $ x_{1},x_{2},... $ , one per line. After the output of each line the program must execute flush operation. The program should read the answer to the query from the standard input.
Your program is allowed to do at most $ 7 $ queries.
Explanation/Hint
The secret string $ s $ in the example is "0123".