CSES - Datatähti 2022 loppu - Results
Submission details
Task:Kanava
Sender:Mahtimursu
Submission time:2022-01-22 15:58:18 +0200
Language:C++ (C++17)
Status:READY
Result:87
Feedback
groupverdictscore
#1ACCEPTED87
Test results
testverdicttimescore
#1ACCEPTED0.01 s87details

Code

#include <bits/stdc++.h>

typedef long long ll;

#define M 1000000007
#define N (1 << 18)

using namespace std;

map<char, string> letter_to_code;
map<string, char> code_to_letter;

void init() {
    letter_to_code['a'] = "00_0";
    letter_to_code['b'] = "00_1";
    letter_to_code['c'] = "01_0";
    letter_to_code['d'] = "01_1";
    letter_to_code['e'] = "10_0";
    letter_to_code['f'] = "10_1";
    letter_to_code['g'] = "0_00";
    letter_to_code['h'] = "0_01";
    letter_to_code['i'] = "0_10";
    letter_to_code['j'] = "0_11";
    letter_to_code['k'] = "1_00";
    letter_to_code['l'] = "1_01";
    letter_to_code['m'] = "1_10";
    letter_to_code['n'] = "1_11";
    letter_to_code['o'] = "0000";
    letter_to_code['p'] = "0001";
    letter_to_code['q'] = "0010";
    letter_to_code['r'] = "0011";
    /*letter_to_code['s'] = "0100";
    letter_to_code['t'] = "0101";
    letter_to_code['u'] = "0110";
    letter_to_code['v'] = "0111";
    letter_to_code['w'] = "1000";
    letter_to_code['x'] = "1001";
    letter_to_code['y'] = "1010";
    letter_to_code['z'] = "1011";*/

    letter_to_code['s'] = "010";
    letter_to_code['t'] = "011";
    letter_to_code['u'] = "100";
    letter_to_code['v'] = "101";
    letter_to_code['w'] = "110";
    letter_to_code['x'] = "111";
    letter_to_code['y'] = "11_0";
    letter_to_code['z'] = "11_1";

    letter_to_code['T'] = "_"; // Terminator

    for (auto [k, v] : letter_to_code) {
        code_to_letter[v] = k;
    }
}

void encode() {
    int k;
    cin >> k;
    vector<string> words(k);
    for (int i = 0; i < k; ++i) {
        cin >> words[i];
    }

    vector<string> res;

    for (auto w : words) {
        int len = w.size();
        string cur;
        for (int i = 0; i < len; ++i) {
            cur += letter_to_code[w[i]];
        }
        cur += letter_to_code['T'];

        res.push_back(cur);
    }

    for (auto x : res) {
        cout << x << endl;
    }
}

void decode() {
    string code;
    cin >> code;

    vector<string> words;
    string curstr;
    string cur;


    for (int i = 0; i < (int)code.size(); ++i) {
        //cout << cur << ", " << curstr << endl;
        if (cur.size() == 4 || (cur.size() == 3 && code_to_letter[cur] >= 's' && code_to_letter[cur] <= 'x')) {
            curstr += code_to_letter[cur];
            cur = "";
        }
        cur += code[i];
        // Terminator
        if (cur == letter_to_code['T']) {
            words.push_back(curstr);
            curstr = "";
            cur = "";
        }
        if (i == (int)code.size() - 1 && cur.size() > 1) {
            curstr += code_to_letter[cur];
            words.push_back(curstr);
        }
    }

    cout << words.size() << "\n";

    for (auto x : words) {
        cout << x << endl;
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t; 
    init();
    if (t == 1) {
        encode();
    } else {
        decode();
    }

    return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
1
520
cbmcsyv
uvub
yh
...

correct output
(empty)

user output
965
ygk
rgzpwpac
ffczhfplny
nwuzdjdj
...
Truncated