Editorial for A Poem Problem


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Editorialist: Tommoa

A Poem Problem is the first problem of the Standard PCS Contest 5. In it, you have a cryptographic cypher which you need to use to either encode or decode a string.

The intended solution is to hold an array or map of characters, which each index mapping to its intended new character. An example implementation in C++ is shown below:

#include <bits/stdc++.h>
using namespace std;

char en[128], de[128];
int N, M;

int main() {
    cin >> N >> M;
    while (N--) {
        char from, to;
        cin >> from >> to;
        en[from] = to;
        de[to] = from;
    }
    while (M--) {
        string t, s;
        cin >> t >> s;
        auto m = t == "E" ? en : de;
        for (auto &c: s) {
            if (m[c] == 0)
                cout << c;
            else
                cout << m[c];
        }
    }
}

Comments

There are no comments at the moment.