one.cpp
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define DEBUG(x) cerr << ">> " << #x << ": " << x << endl;
#define REP(i,a) for (int i =0; i < (a);++i)
#define FOR(i,a,b) for (int i = (a); i <= (b); ++i)
string toMorse[255];
char fromMorse[255];
int tobinary(string s)
{
int a = s.length();
if (s[0] == '-') { a+= 5; }
if (s.length() > 1 && s[1] == '-') { a+= 10; }
if (s.length() > 2 && s[2] == '-') { a+= 20; }
if (s.length() > 3 && s[3] == '-') { a+= 40; }
return a;
}
void addMorse(char c, string s)
{
toMorse[(int)c] = s;
fromMorse[tobinary(s)] = c;
}
int main() {
// while (scanf("%d%d",) != EOF)
// Morse code
addMorse('A', ".-");
addMorse('B', "-...");
addMorse('C', "-.-.");
addMorse('D', "-..");
addMorse('E', ".");
addMorse('F', "..-.");
addMorse('G', "--.");
addMorse('H', "....");
addMorse('I', "..");
addMorse('J', ".---");
addMorse('K', "-.-");
addMorse('L', ".-..");
addMorse('M', "--");
addMorse('N', "-.");
addMorse('O', "---");
addMorse('P', ".--.");
addMorse('Q', "--.-");
addMorse('R', ".-.");
addMorse('S', "...");
addMorse('T', "-");
addMorse('U', "..-");
addMorse('V', "...-");
addMorse('W', ".--");
addMorse('X', "-..-");
addMorse('Y', "-.--");
addMorse('Z', "--..");
addMorse('_', "..--");
addMorse(',', ".-.-");
addMorse('.', "---.");
addMorse('?', "----");
string input;
char morse[4000];
int counts[1000];
while (getline(cin, input, '\n'))
{
int count = 0;
count = input.length();
int curmorse = 0;
for (int i = 0; i < count; i++)
{
string morseChar = toMorse[(int)input[i]];
counts[i] = morseChar.length();
for (int j =0; j < counts[i];j++)
{
morse[curmorse] = morseChar[j];
curmorse++;
}
}
// Loaded to stage 2.
int printwhat = 0;
string morseString(morse);
for (int i = 0; i < count; i++)
{
printf("%c",
fromMorse[
tobinary(
morseString.substr(printwhat, counts[count-i-1]).c_str())]);
printwhat +=counts[count-i-1];
}
printf("\n");
}
return 0;
}