NOI '00 P6 - Symbol Deciphering

View as PDF

Submit solution

Points: 40 (partial)
Time limit: 1.0s
Memory limit: 16M

Problem type
National Olympiad in Informatics, China, 2000

Archaeologists have discovered that the several thousand-year-old mathematics of the ancient Mei civilization was extremely advanced. They understood methods of addition and multiplication for multi-digit numbers. Their rules for representing expressions and performing calculations are exactly the same as those used in the modern day (addition of integers are in base-10, the leftmost digit is the most-significant, the highest digit cannot be zero; expressions are infix, multiplication comes before addition, etc.), with the only exception being that mathematical symbols themselves are written differently. There is sufficient evidence to show that ancient Mei mathematics uses a total of 13 operators which have a one-to-one correspondence to the modern day symbols 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +, *, and =. To simplify the representations of the symbols, we shall use the first 13 alphabetical letters a, b, \dots, m in place of these symbols (known as the ancient Mei mathematical symbols). The problem is, no one currently knows the general relationship for matching the ancient Mei mathematical symbols to modern mathematical symbols.

On a stone wall, archaeologists have discovered a set of equations expressed using the ancient Mei symbols. They have concluded that each row contains only one equal sign, the left and right sides of each equal sign contain mathematical expressions (containing only numbers and operators), and that expressions on the two sides of equal signs are equivalent.

Assuming that this set of equations is successful, please write a program that deciphers the relationship between ancient Mei mathematical symbols and modern mathematical symbols.

Input Specification

The first line of input contains an integer N (1 \le N \le 1\,000). Each of the following N lines contains an equation. Each equation will consist of as few as 5 characters and as many as 11 characters.

Output Specification

If there does not exist a possible relationship that satisfies the set of equations, output noway on a single line.
If there exists a possible mapping that satisfies the set of equations, output all the mappings between ancient Mei symbols and modern symbols that are known for certain. Each line should contain two characters, where the first character is an ancient Mei mathematical symbol and the second character is the modern mathematical symbol to which it is mapped. Output the mappings in alphabetical order.

Sample Input

2
abcdec
cdefe

Sample Output

a6
b*
d=
f+

Explanation

In the sample above, the possibilities for the equations in modern symbols are \{6*2=12, 2=1+1\}, \{6*4=24, 4=2+2\}, and \{6*8=48, 8=4+4\}. It can be seen that the confirmed relationships only consist of a mapping of a to 6, b to *, d to =, and f to +, which should be outputted. Although possible mappings for \{c, e\} can be successfully found, there is no unique relationship for them, and thus they should not be outputted. The other ancient Mei mathematical symbols \{g, h, \dots, m\} are completely unverifiable, and also should not be outputted.

Problem translated to English by Alex.


Comments

There are no comments at the moment.