Description
Morse code represents characters as variable length sequences of dots and dashes. In practice, characters in a message are delimited by short pauses. The following table shows the Morse code sequences:
Note that four dot-dash combinations are unassigned. For the purposes of this problem we will assign them as follows (these are not the assignments for actual Morse code):
Thus, the message "ACM_GREATER_NY_REGION" is encoded as:
.- -.-. -- ..-- --. .-. . .- - . .-. ..-- -. -.-- ..-- .-. . --. .. --- -.
M.E. Ohaver proposed an encryption scheme based on mutilating Morse code. Her scheme replaces the pauses between letters, necessary because Morse is a variable-length encoding that is not prefix-free, with a string that identifies the number of dots and dashes in each. For example, consider the message ".--.-.--". Without knowing where the pauses should be, this could be "ACM", "ANK", or several other possibilities. If we add length information, however, ".--.-.--242", then the code is unabiguous.
Ohaver's scheme has three steps, the same for encryption and decryption:
1. Convert the text to Morse code without pauses but with a string of numbers to indicate code lengths
2. Reverse the string of numbers
3. Convert the dots and dashes back into to text using the reversed string of numbers as code lengths
As an example, consider the encrypted message "AKADTOF_IBOETATUK_IJN". Converting to Morse code with a length string yields ".--.-.--..----..-...--..-...---.-.--..--.-..--...----.232313442431121334242". Reversing the numbers and decoding yields the original message "ACM_GREATER_NY_REGION".
A | .- | H | .... | O | --- | V | ...- |
B | -... | I | .. | P | .--. | W | .-- |
C | -.-. | J | .--- | Q | --.- | X | -..- |
D | -.. | K | -.- | R | .-. | Y | -.-- |
E | . | L | .-.. | S | ... | Z | --.. |
F | ..-. | M | -- | T | - | ||
G | --. | N | -. | U | ..- |
Note that four dot-dash combinations are unassigned. For the purposes of this problem we will assign them as follows (these are not the assignments for actual Morse code):
underscore | ..-- | period | ---. |
comma | .-.- | question mark | ---- |
Thus, the message "ACM_GREATER_NY_REGION" is encoded as:
.- -.-. -- ..-- --. .-. . .- - . .-. ..-- -. -.-- ..-- .-. . --. .. --- -.
M.E. Ohaver proposed an encryption scheme based on mutilating Morse code. Her scheme replaces the pauses between letters, necessary because Morse is a variable-length encoding that is not prefix-free, with a string that identifies the number of dots and dashes in each. For example, consider the message ".--.-.--". Without knowing where the pauses should be, this could be "ACM", "ANK", or several other possibilities. If we add length information, however, ".--.-.--242", then the code is unabiguous.
Ohaver's scheme has three steps, the same for encryption and decryption:
1. Convert the text to Morse code without pauses but with a string of numbers to indicate code lengths
2. Reverse the string of numbers
3. Convert the dots and dashes back into to text using the reversed string of numbers as code lengths
As an example, consider the encrypted message "AKADTOF_IBOETATUK_IJN". Converting to Morse code with a length string yields ".--.-.--..----..-...--..-...---.-.--..--.-..--...----.232313442431121334242". Reversing the numbers and decoding yields the original message "ACM_GREATER_NY_REGION".
Input
This problem requires that you implement Ohaver's encoding algorithm. The input will consist of several messages encoded with Ohaver's algorithm. The first line of the input is an integer n that specifies the number of test cases. The following n lines contain one message per line. Each message will use only the twenty-six capital letters, underscores, commas, periods, and question marks. Messages will not exceed 100 characters in length.
Output
For each message in the input, output the line number starting in column one, a colon, a space, and then the decoded message. The output format must be adhered to precisely.
Sample Input
5 AKADTOF_IBOETATUK_IJN PUEL QEWOISE.EIVCAEFNRXTBELYTGD. ?EJHUT.TSMYGW?EJHOT DSU.XFNCJEVE.OE_UJDXNO_YHU?VIDWDHPDJIKXZT?E
Sample Output
1: ACM_GREATER_NY_REGION 2: PERL 3: QUOTH_THE_RAVEN,_NEVERMORE. 4: TO_BE_OR_NOT_TO_BE? 5: THE_QUICK_BROWN_FOX_JUMPS_OVER_THE_LAZY_DOG
思路:
首先想到的是用一个map存储Morse code,然后读入一个加密的Morse code, 从加密的Morse code中一个一个的比对map中的value,将value连续存入一个string中,并将value的长度读入vector中,这样就获得了Length information,接着从vector中从尾到头获得Length information,根据这个length information,从之前的string中获得新的value,根据这个value从Morse map中查找对应的key,将这个key存入result中,当vector中循环结束,即获得解密的密码。
代码:
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
// set a morse map
void setMorse(map<char, string>& M){
M.insert(pair<char, string>('A', ".-"));
M.insert(pair<char, string>('B', "-..."));
M.insert(pair<char, string>('C', "-.-."));
M.insert(pair<char, string>('D', "-.."));
M.insert(pair<char, string>('E', "."));
M.insert(pair<char, string>('F', "..-."));
M.insert(pair<char, string>('G', "--."));
M.insert(pair<char, string>('H', "...."));
M.insert(pair<char, string>('I', ".."));
M.insert(pair<char, string>('J', ".---"));
M.insert(pair<char, string>('K', "-.-"));
M.insert(pair<char, string>('L', ".-.."));
M.insert(pair<char, string>('M', "--"));
M.insert(pair<char, string>('N', "-."));
M.insert(pair<char, string>('O', "---"));
M.insert(pair<char, string>('P', ".--."));
M.insert(pair<char, string>('Q', "--.-"));
M.insert(pair<char, string>('R', ".-."));
M.insert(pair<char, string>('S', "..."));
M.insert(pair<char, string>('T', "-"));
M.insert(pair<char, string>('U', "..-"));
M.insert(pair<char, string>('V', "...-"));
M.insert(pair<char, string>('W', ".--"));
M.insert(pair<char, string>('X', "-..-"));
M.insert(pair<char, string>('Y', "-.--"));
M.insert(pair<char, string>('Z', "--.."));
M.insert(pair<char, string>('_', "..--"));
M.insert(pair<char, string>('.', "---."));
M.insert(pair<char, string>(',', ".-.-"));
M.insert(pair<char, string>('?', "----"));
}
int main(){
map<char, string> Morse;
setMorse(Morse);
char c;
int num;
int flag = 1;
cin >> num;
while (num--){
vector<int> vec;
string morseKeyStr;
string morseValueStr;
cin >> morseKeyStr;
for (int i = 0; i != morseKeyStr.size(); ++i){
c = morseKeyStr[i];
morseValueStr += Morse[c];
int len = Morse[c].size(); //store length info
vec.push_back(len);
}
int i = 0;
string resultStr;
for (int pos = vec.size() - 1; pos >= 0; --pos){ //reverse the length info
string strTemp = morseValueStr.substr(i, vec[pos]); //get substr according to the reversed length info
for (map<char, string>::const_iterator it = Morse.begin(); it != Morse.end(); ++it){ //seek the key matches to the value from substr
if (it->second == strTemp){
resultStr += it->first; //store the key into the resultStr
break;
}
}
i = i + vec[pos]; //get another substr
}
cout << flag << ": " << resultStr << endl;
flag++;
}
return 0;
}