This page is still under construction.

Parts of this page are still being built. What you see may change.

Mõttemeister

Interview

Time limit1sMemory limit1024 MB

Summary
Given several guesses and their correct-digit counts A and position counts B, list every 4-digit secret number consistent with all clues.
Level

Medium5 of 10

Topics
Brute force, Implementation, Simulation, Array
Solved
No attempts yet

Statement

Mõttemeister is a board game for two players. The first player thinks of a secret 4-digit number, and the second player tries to guess it.

On each turn the guesser proposes a 4-digit number. The first player answers with two values AA and BB, where AA is how many of the digits in the proposed number are correct, and BB is how many of those are also in the correct position.

The guesser then makes a new proposal, and the game continues until the guesser finds the secret number or the number of turns exceeds a given limit.

For example, suppose the secret number is 52475247. For the proposal 12341234 the answer would be 22 (the digits 22 and 44 are correct) and 11 (the digit 22 is in the correct position).

If a proposal contains repeated digits, each digit is counted as correct only as many times as it occurs in the secret number.

Write a program that, from the given proposals and answers, finds all possible values of the secret number.

Input

The first line contains an integer NN (1≤N≤10 0001 \le N \le 10\,000). Each of the next NN lines describes one turn: the proposed 4-digit number, its count of correct digits AA (0≤A≤40 \le A \le 4), and the count of those digits that are also in the correct position BB (0≤B≤A0 \le B \le A).

Output

On the first line print the number MM of possible values of the secret number. On the next MM lines print the possible secret numbers in increasing order, one per line. Every number is printed with exactly four digits, padded with leading 00s if necessary.

Examples2

  1. Example 1

    Input
    1
    1234 4 4
    
    Expected output
    1
    1234
    
  2. Example 2

    Input
    2
    0000 1 1
    1111 3 3
    
    Expected output
    4
    0111
    1011
    1101
    1110