Character Decoding
This is a test for decoding characters’ values. Assume a numerical expression is encoded in English characters by replacing some digit numbers (from 0 to 9) with English characters. So this kind of numerical expressions can be expressed in new forms, such as
2BAD = ABE + CD
Please write a program to decode the expressions in characters and output the numerical value of characters, according to the following rules and assumptions.
- All character values are integers between 0 to 9 both inclusive.
- An expression is represented as a set of items combined with operators. Only the operators ‘+’,
‘- and ‘=’ are used in each expression. And at most 5 items are used in one expression.
- There is one and only one operator ‘=’ in each expression. And only one item is in the left-hand
side of the operator ‘=’.
- Each item is represented by a combination of capital English characters and digital numbers. The
value of the left-most character in each item is not 0.
- The input data are represented as several rows of numerical expressions and are stored in a file. Each row is an independent expression with other rows. The end of the input file is a star symbol (*).
- Output the value of the left-most item in each expression row by row, in the same order as that in the input file.
- If there are multiple solutions, print out the smallest values for each left-most item. If no possible solutions exist, print out a question mark (?) instead.
Input
Contains k lines, with k − 1 expressions.
Line 1
...
Line k−1 Line k
Output
the first expression the (k−1)-th
A star symbol (*) indicating end-of-file
Contains k − 1 lines. Each line is the smallest value that satisfies the corresponding expression.
Line 1 value ...
Line k − 1 value
2/2 Sample Input
CA = AB + 6C
DDE5 = DEFG - EHI + DDH A=0
Sample Output
81 1115 ?