fs.cpp
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <string>
#include <map>
int i, j, k;
int i2, j2, k2;
std::map<char, std::string> toMorse;
std::map<std::string, char> fromMorse;
char line[1024];
std::string morse;
int morseLen[1024];
int fillTables()
{
toMorse['A'] = ".-";
toMorse['B'] = "-...";
toMorse['C'] = "-.-.";
toMorse['D'] = "-..";
toMorse['E'] = ".";
toMorse['F'] = "..-.";
toMorse['G'] = "--.";
toMorse['H'] = "....";
toMorse['I'] = "..";
toMorse['J'] = ".---";
toMorse['K'] = "-.-";
toMorse['L'] = ".-..";
toMorse['M'] = "--";
toMorse['N'] = "-.";
toMorse['O'] = "---";
toMorse['P'] = ".--.";
toMorse['Q'] = "--.-";
toMorse['R'] = ".-.";
toMorse['S'] = "...";
toMorse['T'] = "-";
toMorse['U'] = "..-";
toMorse['V'] = "...-";
toMorse['W'] = ".--";
toMorse['X'] = "-..-";
toMorse['Y'] = "-.--";
toMorse['Z'] = "--..";
toMorse['_'] = "..--";
toMorse[','] = ".-.-";
toMorse['.'] = "---.";
toMorse['?'] = "----";
for (std::map<char, std::string>::iterator it = toMorse.begin(); it != toMorse.end(); it++)
{
fromMorse[it->second] = it->first;
}
return 0;
}
/* ------------------------------- */
int main()
{
fillTables();
int len;
while (scanf("%s", line) == 1)
{
morse = "";
len = strlen(line);
for (i = 0; i < len; i++)
{
std::string& s = toMorse[line[i]];
morse += s;
morseLen[i] = s.length();
}
/*
printf("%s", morse.c_str());
for (i = 0; i < len; i++)
{
printf("%d", morseLen[i]);
}
printf("\n");
*/
int pos = 0;
for (i = len-1; i >= 0; i--)
{
std::string found;
for (j = 0; j < morseLen[i]; j++)
found += morse[pos + j];
putchar(fromMorse[found]);
pos += morseLen[i];
}
printf("\n");
}
return 0;
}