CCC '99 S5 - Letter Arithmetic

View as PDF

Submit solution

Points: 12
Time limit: 1.0s
Memory limit: 16M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

A popular form of pencil game is to use letters to represent digits in a mathematical statement. An example is

 SEND
+MORE
-----
MONEY

which represents

 9567
+1085
-----
10652

Your task is to read in sets of three "words" and assign unique digits to the letters in such a way as to make the sum of the first two words equal to the third word.

Input Specification

The input begins with a line containing a positive integer n which is the number of test data sets. Each data set consists of three lines, each of which contains one word with the third word being the sum of the first two. The words will contain no more than 20 upper case letters.

Output Specification

The output is to consist of n sets of lines each containing the numeric representation of each word in the corresponding test data set. There will be exactly one correct solution for each data set. Leave an empty line after the output for each data set.

Sample Input

2
SEND
MORE
MONEY
MEND
COPE
CONEY

Sample Output

9567
1085
10652
9567
1085
10652

Comments


  • 8
    Kirito  commented on Sept. 10, 2016, 5:41 p.m.

    0 may not be the first digit of any number. (E.g. in the example with SEND+MORE=MONEY, S, M \neq 0)