用两个map存储加密和解密信息。
一个stack<int>存储字符长度,加密时长度入栈,解密时长度出栈,符合先进后出。
#pragma warning (disable : 4786)
#include<iostream>
#include<string>
#include<map>
#include<cstdio>
#include<stack>
using namespace std;
map<char,string> morse;
map<string,char> remorse;
stack<int> num;
void init(){
morse['A'] = ".-";
morse['B'] = "-...";
morse['C'] = "-.-.";
morse['D'] = "-..";
morse['E'] = ".";
morse['F'] = "..-.";
morse['G'] = "--.";
morse['H'] = "....";
morse['I'] = "..";
morse['J'] = ".---";
morse['K'] = "-.-";
morse['L'] = ".-..";
morse['M'] = "--";
morse['N'] = "-.";
morse['O'] = "---";
morse['P'] = ".--.";
morse['Q'] = "--.-";
morse['R'] = ".-.";
morse['S'] = "...";
morse['T'] = "-";
morse['U'] = "..-";
morse['V'] = "...-";
morse['W'] = ".--";
morse['X'] = "-..-";
morse['Y'] = "-.--";
morse['Z'] = "--..";
morse['_'] = "..--";
morse['.'] = "---.";
morse[','] = ".-.-";
morse['?'] = "----";
remorse[".-"] = 'A';
remorse["-..."] = 'B';
remorse["-.-."] = 'C';
remorse["-.."] = 'D';
remorse["."] = 'E';
remorse["..-."] = 'F';
remorse["--."] = 'G';
remorse["...."] = 'H';
remorse[".."] = 'I';
remorse[".---"] = 'J';
remorse["-.-"] = 'K';
remorse[".-.."] = 'L';
remorse["--"] = 'M';
remorse["-."] = 'N';
remorse["---"] = 'O';
remorse[".--."] = 'P';
remorse["--.-"] = 'Q';
remorse[".-."] = 'R';
remorse["..."] = 'S';
remorse["-"] = 'T';
remorse["..-"] = 'U';
remorse["...-"] = 'V';
remorse[".--"] = 'W';
remorse["-..-"] = 'X';
remorse["-.--"] = 'Y';
remorse["--.."] = 'Z';
remorse["..--"] = '_';
remorse["---."] = '.';
remorse[".-.-"] = ',';
remorse["----"] = '?';
}
int main(){
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
init();
int N;
cin>>N;
for(int i=0;i<N;i++){
cout<<i+1<<": ";
string input;
cin>>input;
string trans="";
num.empty();
for(int i=0;i<input.size();i++){
trans += morse[input.at(i)];
num.push(morse[input.at(i)].size());
}
// cout<<trans<<endl;
int index = 0;
while(!num.empty()){
string output="";
// cout<<num.top();
int n = num.top();
while(n>0){
output += trans[index];
index++;
n--;
}
// cout<<output;
cout<<remorse[output];
num.pop();
}
cout<<endl;
}
return 0;
}