-
Notifications
You must be signed in to change notification settings - Fork 2
/
11223.cpp
99 lines (95 loc) · 2.22 KB
/
11223.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
Solution to 11223
*/
#include<iostream>
#include<map>
#include<string>
using namespace std;
map<string, char> decode;
void encode();
int main()
{
int cases;
string message;
encode();
cin >> cases;
cin.ignore();
for(int num = 1; num <= cases; num++)
{
getline(cin, message);
if(num > 1)
cout << endl;
cout << "Message #" << num << endl;
for(int i = 0; i < message.size(); i++)
{
//Check to see if its the start of a new word
if(i > 0 && message[i] == message[i-1] && message[i] == ' ')
cout << ' ';
//Otherwise, get decode next character
else
{
//find returns -1 if it fails which will only happen at the end of the string
int space = message.find(' ', i);
cout << decode[message.substr( i, space-i )];
i = space == -1 ? message.size() : space;
}
}
cout << endl;
}
}
void encode()
{
decode[".-"] = 'A';
decode["-..."]= 'B';
decode["-.-."] = 'C';
decode["-.."] = 'D';
decode["."] = 'E';
decode["..-."] = 'F';
decode["--."] = 'G';
decode["...."] = 'H';
decode[".."] = 'I';
decode[".---"] = 'J';
decode["-.-"] = 'K';
decode[".-.."] = 'L';
decode["--"] = 'M';
decode["-."] = 'N';
decode["---"] = 'O';
decode[".--."] = 'P';
decode["--.-"] = 'Q';
decode[".-."] = 'R';
decode["..."] = 'S';
decode["-"] = 'T';
decode["..-"] = 'U';
decode["...-"] = 'V';
decode[".--"] = 'W';
decode["-..-"] = 'X';
decode["-.--"] = 'Y';
decode["--.."] = 'Z';
decode["-----"] = '0';
decode[".----"] = '1';
decode["..---"] = '2';
decode["...--"] = '3';
decode["....-"] = '4';
decode["....."] = '5';
decode["-...."] = '6';
decode["--..."] = '7';
decode["---.."] = '8';
decode["----."] = '9';
decode[".-.-.-"] = '.';
decode["--..--"] = ',';
decode["..--.."] = '?';
decode[".----."] = '\'';
decode["-.-.--"] = '!';
decode["-..-."] = '/';
decode["-.--."] = '(';
decode["-.--.-"] = ')';
decode[".-..."] = '&';
decode["---..."] = ':';
decode["-.-.-."] = ';';
decode["-...-"] = '=';
decode[".-.-."] = '+';
decode["-....-"] = '-';
decode["..--.-"] = '_';
decode[".-..-."] = '"';
decode[".--.-."] = '@';
}