cppAcademy
FROM LEARNING TO COMPETING

Beginner.
Your first challenges.

Bulgarian Informatics Group E · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad tasks and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group E also includes challenging national-level problems. Years follow Arena’s competition-season labels. Source clarifications are noted on affected tasks.
GROUP E2011 · MediumStrings & text

Hidden Messages

National Olympiad — National round (2011 season)

Submission name: message.cpp

Problem summary

Given a substitution table, decode a message. Each table row contains a decoded character, one separating space, and its encoded character. A literal space may itself be either character, so preserve the exact row positions.

Input

N; then N three-character table rows decoded-space-encoded; then one full encoded message line.

Output

The decoded message, preserving all decoded spaces.

Constraints

Message length ≤ 80. Characters are uppercase Latin letters, digits, space, =, <, >, period, comma, and !.

Worked example

An independently prepared example for this explanation.

Input

15
A 5
B B
C 4
D Q
  F
I L
J T
K  
M 7
E Z
N 8
5 >
. X
9 E
O Y
4Y7ZFL8

Output

COME IN

Solution analysis

  1. Read each mapping row with getline. The decoded symbol is row[0], the separator is row[1], and the encoded symbol is row[2]. Token extraction would skip meaningful spaces.
  2. Store a reverse lookup from encoded character to decoded character. Read the entire final message line and translate each character through that lookup.
  3. In the sample, 4 maps to C, Y to O, 7 to M, Z to E, F to a space, L to I, and 8 to N. Thus 4Y7ZFL8 becomes COME IN. The row for K maps an encoded space to K.

Why it works

For each table row the reverse lookup stores exactly the decoded character corresponding to its encoded character. Each message position is independently replaced using that lookup, so every output character is the correct decoding of the matching input position, including spaces.

Complexity: O(N + L) time for N mappings and a message of length L; O(1) lookup space plus O(L) message space.

Watch out. Do not trim rows or the message. Use getline rather than >> for character pairs; an encoded trailing space is data.

Commented C++17 solution

An independently written complete program with standard headers.

#include <iostream>
#include <string>
using namespace std;
int main() {
    int n;
    cin >> n;
    string line;
    getline(cin, line);
    char decoded[256];
    for (int i = 0; i < 256; i++) {
        decoded[i] = static_cast<char>(i);
    }
    for (int i = 0; i < n; i++) {
        getline(cin, line);
        if (!line.empty() && line.back() == '\r') {
            line.pop_back();
        }
        // Preserve literal spaces at either end of a mapping row.
        if (line.size() < 3) {
            line.resize(3, ' ');
        }
        decoded[static_cast<unsigned char>(line[2])] = line[0];
    }
    string message;
    getline(cin, message);
    if (!message.empty() && message.back() == '\r') {
        message.pop_back();
    }
    for (unsigned char character : message) {
        cout << decoded[character];
    }
    cout << '\n';
}

Source: Arena Olimpiici ↗