Mastermind II
Time limit1sMemory limit128 MB
Given c codes and their A/B compatibility scores with a hidden code of length c, find the lexicographically smallest code consistent with all scores.
- Level
Medium7 of 10
- Topics
- Brute force, Backtracking, Implementation, Combinatorics
- Solved
- No attempts yet
Problem
Consider sequences that satisfy all of the following conditions:
- the length of the sequence is ;
- every element is a digit from to ;
- no digit is repeated within the sequence.
Such a sequence is called a code.
Given two codes, we measure their compatibility with two numbers:
- is the sum of the digits that appear in both codes at the same position.
- is the sum of the digits that appear in both codes but at different positions.
There is one fixed but unknown code. You are given codes together with the compatibility that each of them has with this unknown code. Find a code that is consistent with all of these compatibility values.
Input
The first line contains one integer ().
Each of the next lines describes one given code together with its compatibility with the unknown code. A line contains non-negative integers separated by single spaces: the first two are the compatibility values and of this code, and the remaining integers are the distinct digits (each from to ) that form the code.
Output
Print digits separated by single spaces: a code (distinct digits from to ) whose compatibility with every given code matches the input.
It is guaranteed that at least one such code exists. If several codes satisfy all the constraints, print the lexicographically smallest one (compare the two codes digit by digit from left to right).