DWITE Online Computer Programming Contest

Word Arithmetic

November 2012
Problem 2

Tommy always gets really bored during his English class. However, Tommy loves math! He spends most of his free time adding two numbers together (it’s actually really exciting!). One day he came up with a brilliant idea to both cure his boredom in English class and to not get caught by his teacher: add words together! The way Tommy adds two words together is exactly how he would add two numbers:

  1. Every letter represents a number: A = 0, B = 1, ..., Z = 25.
  2. Add the words one letter at a time from right to left.
  3. If the sum of two letters overflows (that is, greater than Z = 25), then the letter to write down will be the sum modulo 26 (i.e. the remainder), and the carry will be the quotient you get when you divide the sum by 26. (So essentially the two numbers are in base 26 instead of base 10.)

For example, Z + Z = BY (since Z + Z = 50, and 50 modulo 26 is 24, which is Y, and the carry is the quotient you get when you divide 50 by 26, i.e. 1, which represents B).

Note that you don’t want the answer to have any leading A’s (as that’s equivalent to having leading 0’s).

The input file DATA2.txt will contain 5 test cases. Each case is a line with two words, W1 and W2, separated by a single space. Every word is at least 1 letter long and no more than 254, made up of only uppercase letters.

The output file OUT2.txt will contain 5 lines of output. Each is a single word representing W1 + W2, spelled in upper case.

Sample Input (first 3 shown):
CAT DOG
MOM DAD
ABA A
Sample Output (first 3 shown):
FOZ
POP
BA

Note on the third case: Words can be of different lengths. A = 0 so ABA + 0 shouldn’t change, but leading 0’s are removed. Since 01026 = 1026, the answer is 1026 (that is, BA).