Tuesday, April 21, 2015

UVa 11223 - O: dah dah dah!

// UVa 11223 - O: dah dah dah!

#include <stdio.h>
#include <sstream>
#include <iostream>
#include <map>
#include <string>
using namespace std;

map<string, string> decode;

string key = "A .- J .--- S ... 1 .---- . .-.-.- : ---... " + "B -... K -.- T - 2 ..--- ; -.-.-. " + "C -.-. L .-.. U ..- 3 ...-- ? ..--.. = -...- " + "D -.. M -- V ...- 4 ....- ' .----. + .-.-. "
		+ "E . N -. W .-- 5 ..... ! -.-.-- - -....- " + "F ..-. O --- X -..- 6 -.... / -..-. _ ..--.- " + "G --. P .--. Y -.-- 7 --... ( -.--. \" .-..-. "
		+ "H .... Q --.- Z --.. 8 ---.. ) -.--.- @ .--.-. " + "I .. R .-. 0 ----- 9 ----. & .-...";

void init_morse_map() {
	istringstream istrm(key);
	string a, b;
	while (istrm >> a >> b) {
		decode[b] = a;
	}
	decode["--..--"] = ",";
	decode["  "] = " ";
}

int main() {
	init_morse_map();
	int cases;
	scanf("%d\n", &cases);
	for (int cas = 1; cas <= cases; cas++) {
		char ch;
		string message, previous = "";
		for (scanf("%c", &ch); ch != 10 && ch != 13; scanf("%c", &ch)) {
			if (ch == '.' || ch == '-') {
				if (previous == " ")
					previous = "";
				previous += ch;
			} else if (ch == ' ' && previous == " ") {
				previous = "";
				message += " ";
			} else {
				message += decode[previous];
				previous = ch == ' ' ? " " : "";
			}
		}
		message += decode[previous];
		if (cas > 1)
			printf("\n");
		printf("Message #%d\n%s\n", cas, message.c_str());
	}
	return 0;
}

No comments:

Post a Comment