#P1092. Worm-eaten Arithmetic Addition
Worm-eaten Arithmetic Addition
Worm-eaten Arithmetic Addition
In this problem, you are given an addition equation in base n in which all three numbers (the two operands and the sum) have exactly n digits. The digits are represented by the first n uppercase letters (A, B, C, ...), but the correspondence between a letter and a digit (from 0 to n-1) is unknown. Moreover, each letter appears at least once in the entire equation. Two identical letters in the equation represent the same digit, and distinct letters represent different digits.
You are required to determine the unique digit mapping so that the following equation holds in base n:
For example, consider the 4-base puzzle:
$$\begin{aligned} \verb!BADC! \\ +\quad \verb!CBDA! \\ \underline{\kern{4em}} \\ \verb!DCCC! \end{aligned} $$The correct solution for the mapping is:
$$\text{A}=0,\, \text{B}=1,\, \text{C}=2,\, \text{D}=3. $$Your task is to find the digit for each letter (in the order A, B, C, ... up to the nth letter) such that the above addition holds, knowing that there is exactly one solution.
inputFormat
The first line of input contains an integer n (2 ≤ n ≤ 10), which is also the number of distinct letters (the first n uppercase letters) used in the equation. It is guaranteed that each of these n letters appears at least once in the equation.
The next three lines each contain a string of length n representing the first operand, the second operand, and the result respectively. These strings represent numbers in base n (digits may include leading zeros).
outputFormat
Output a single line containing n integers separated by spaces. The i-th integer (starting from i = 0) is the digit (in base n) corresponding to the i-th letter in the alphabetical order (i.e. A, B, C, ...).
sample
4
BADC
CBDA
DCCC
0 1 2 3