fs.cpp
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
map<char, string> morse;
map<string, char> morse2;
int main()
{
morse['A'] = ".-";
morse['B'] = "-...";
morse['C'] = "-.-.";
morse['D'] = "-..";
morse['E'] = ".";
morse['F'] = "..-.";
morse['G'] = "--.";
morse['H'] = "....";
morse['I'] = "..";
morse['J'] = ".---";
morse['K'] = "-.-";
morse['L'] = ".-..";
morse['M'] = "--";
morse['N'] = "-.";
morse['O'] = "---";
morse['P'] = ".--.";
morse['Q'] = "--.-";
morse['R'] = ".-.";
morse['S'] = "...";
morse['T'] = "-";
morse['U'] = "..-";
morse['V'] = "...-";
morse['W'] = ".--";
morse['X'] = "-..-";
morse['Y'] = "-.--";
morse['Z'] = "--..";
morse['_'] = "..--";
morse[','] = ".-.-";
morse['.'] = "---.";
morse['?'] = "----";
morse2[".-"] = 'A';
morse2["-..."] = 'B';
morse2["-.-."] = 'C';
morse2["-.."] = 'D';
morse2["."] = 'E';
morse2["..-."] = 'F';
morse2["--."] = 'G';
morse2["...."] = 'H';
morse2[".."] = 'I';
morse2[".---"] = 'J';
morse2["-.-"] = 'K';
morse2[".-.."] = 'L';
morse2["--"] = 'M';
morse2["-."] = 'N';
morse2["---"] = 'O';
morse2[".--."] = 'P';
morse2["--.-"] = 'Q';
morse2[".-."] = 'R';
morse2["..."] = 'S';
morse2["-"] = 'T';
morse2["..-"] = 'U';
morse2["...-"] = 'V';
morse2[".--"] = 'W';
morse2["-..-"] = 'X';
morse2["-.--"] = 'Y';
morse2["--.."] = 'Z';
morse2["..--"] = '_';
morse2[".-.-"] = ',';
morse2["---."] = '.';
morse2["----"] = '?';
unsigned int len[1020];
char encoded[4020];
char decoded[1020];
uint encoded_len;
string line;
while(getline(cin, line))
{
encoded_len = 0;
for (uint i = 0; i < line.size(); ++i)
{
string& ch = morse[line[i]];
len[i] = ch.size();
for (uint j = 0; j < ch.size(); ++j)
{
encoded[encoded_len++] = ch[j];
}
}
reverse(len, len + line.size());
uint encoded_pos = 0;
for (uint i = 0; i < line.size(); ++i)
{
string ch;
for (uint j = 0; j < len[i]; ++j)
{
ch.push_back(encoded[encoded_pos++]);
}
decoded[i] = morse2[ch];
}
decoded[line.size()] = 0;
printf("%s\n", decoded);
}
}