Connection Status:
Competition Arena > CryptoDigit
SRM 118 · 2002-10-29 · by dgoodman
Class Name: CryptoDigit
Return Type: String
Method Name: add
Arg Types: (string, string, string)
Problem Statement

Problem Statement

Given a mapping from the digits '0','1',...,'9' to some 10 characters, we want to be able to calculate the sum of two numbers (in base 10) that are already mapped and express it under the mapping.

Create a class CryptoDigit that contains the method add that takes a String, mapping, representing the mapping of digits, and two Strings representing numbers under the mapping as inputs and returns a String which is the sum (under the mapping). mapping will be a String with exactly 10 characters, where character i represents the character that the ith digit maps to.

Notes

  • mapping will contain the 10 mapped characters corresponding to our '0','1',...,'9' in that order.
  • The returned String should not have superfluous leading mapped zeroes (see Example 0).
  • num1 and num2 may contain leading mapped zeroes.

Constraints

  • mapping contains exactly 10 characters, all distinct
  • mapping contains only lowercase letters a-z and digits 0-9
  • num1 and num2 contain only characters that appear in mapping
  • num1 contains between 1 and 9 characters, inclusive.
  • num2 contains between 1 and 9 characters, inclusive.
Examples
0)
"0123456789"
"91"
"0009"
Returns: "100"

This is the identity mapping.

1)
"9876543210"
"91"
"0009"
Returns: "0001"

num1 is what we call "8" and num2 is what we call "9990", so their sum is what we call "9998" which is 0001 under the mapping.

2)
"abcdefghij"
"jjjjjjjjj"
"jjjjjjjjj"
Returns: "bjjjjjjjji"
3)
"abcdefghij"
"a"
"aaaab"
Returns: "b"
4)
"abcdefghij"
"j"
"b"
Returns: "ba"
5)
"ab7defg9i2"
"aa"
"aaa"
Returns: "a"

This leading mapped zero is necessary.

Submissions are judged against all 13 archived test cases, of which 6 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class CryptoDigit with a public method string add(string mapping, string num1, string num2) · 13 test cases · 2 s / 256 MB per case

Submitting as anonymous