一道模拟题,要进行相应的转换,
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#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()
{
int T,cas=1;
Init();
char str[1001];
scanf("%d",&T);
while(T--)
{
printf("%d: ",cas++);
scanf("%s",str);
string s_morse="",ans="";
num.empty();
for(int i=0;i<strlen(str);i++)
{
s_morse=s_morse+morse[str[i]];
num.push(morse[str[i]].length());
}
int n=0,a=0;
while(!num.empty())
{
a=num.top();
num.pop();
ans="";
while(a--)
ans=ans+s_morse[n++];
cout<<remorse[ans];
}
cout<<endl;
}
return 0;
}