ZeroJudge J010: Code Number

UVa 11946 – Code Number

Adrian and Maria are relatives living in different towns. Due to residing in rural areas, they find it difficult to stay in touch. They discovered that one way to overcome the communication barrier is by relaying messages through their parents' phones. The key is that Adrian and Maria do not want their parents to read their messages, so they decided to create a code for these messages.

The code is not very complicated, but you should remember that Adrian and Maria are just children. Generally, the meaning of the message is based on changing some letters with numbers.

Each message consists of several lines, using uppercase English letters, spaces, and punctuation marks: periods and commas. The letters changed by numbers can be seen in the following example.

Message in “Code Number”:

H3LL0 MY L0V3, 1 M H499Y 83C4U53 500N 1 W1LL 83 70 Y0UR 51D3.
7H15 71M3 W17H0U7 Y0U H45 833N 373RN4L. 1 1NV173 Y0U 70 7H3 200
0N3 70 533 7H3 238R42 4ND 60R1L45.

Decode Message:

HELLO MY LOVE, I M HAPPY BECAUSE SOON I WILL BE TO YOUR SIDE.
THIS TIME WITHOUT YOU HAS BEEN ETERNAL. I INVITE YOU TO THE ZOO
ONE TO SEE THE ZEBRAS AND GORILAS.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The first line contains a number T (1 ≤ T ≤ 30), representing the number of test cases.
Each test case is separated by a blank line. Each test case is a message encrypted with a Code Number. There are 1 ≤ total number of lines ≤ 100, and each line will not exceed 80 characters.
For each test case, please output the decoded message, with a blank line between each test case.
1
H3LL0 MY L0V3, 1 M H499Y 83C4U53 500N 1 W1LL 83 70 Y0UR 51D3. 7H15
71M3 W17H0U7 Y0U H45 833N 373RN4L. 1 1NV173 Y0U 70 7H3 200 0N3 70
533 7H3 238R45 4ND 60R1L45.
HELLO MY LOVE, I M HAPPY BECAUSE SOON I WILL BE TO YOUR SIDE. THIS
TIME WITHOUT YOU HAS BEEN ETERNAL. I INVITE YOU TO THE ZOO ONE TO
SEE THE ZEBRAS AND GORILAS.
ZeroJudge J010 Sample Test Case

Thought Process

Use while(getline(cin, line) && line != "") to read each string of the test case data. The loop will end when an empty line is encountered. You can use a map to record the English letter corresponding to each number. By using map, you only need to check if Map[character] is "". If it is "", it means there is no data inside, so you directly output the current character. If it has a value, output the corresponding English letter.

Sample Code-ZeroJudge J010: Code Number

#include <iostream>
#include <map>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    map<char, string>MAP;
    MAP['3'] = 'E';
    MAP['0'] = 'O';
    MAP['1'] = 'I';
    MAP['4'] = 'A';
    MAP['9'] = 'P';
    MAP['8'] = 'B';
    MAP['5'] = 'S';
    MAP['7'] = 'T';
    MAP['2'] = 'Z';
    MAP['6'] = 'G';
    int T;
    cin >> T;
    string s;
    getline(cin, s);
    for (int i = 0; i<T; i++) {
        string str;
        while (getline(cin, str) && str != "") {
            for (int j = 0; j<str.length(); j++) {
                if (MAP[str[j]] == "") cout << str[j];
                else cout << MAP[str[j]];
            }
            cout << "\n";
        }
    }
}

//ZeroJudge J010
//Dr. SeanXD

To Benny: Happy 17th Birthday!

Comments