题意:输入字符串,转换为morse码,求将该morse码对应的数字串反转后所对应的morse码。
例如:AKADTOF_IBOETATUK_IJN先转为morse码
.--.-.--..----..-...--..-...---.-.--..--.-..--...----.232313442431121334242
将数字翻转为242433121136266313232,此时morse码为:
.--.-.--..----..-...--..-...---.-.--..--.-..--...----.242433121136266313232
最后再转为文本ACM_GREATER_NY_REGION
算法:模拟,水题。
#include <iostream>
#include <map>
#include <string>
using namespace std;
map <char, string> morse;
map <string, char> remorse;
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 cases;
string str, ans,morse_str;
int p[110];
init();
cin >> cases;
for (int i=1; i<=cases; i++)
{
cin >> str;
morse_str = "";
int len = str.length();
cout << i << ": ";
// 将输入的字符串转换为morse码
for (int j=0; j<len; j++)
{
morse_str += morse[str[j]];
p[j] = morse[str[j]].length();
}
// 按数字逆序输出morse码对应的文本
int start=0;
for (int j=len-1; j>=0; j--)
{
cout << remorse[morse_str.substr(start,p[j])] ;
start += p[j];
}
cout << endl;
}
}