poj1051 模拟

题意:输入字符串,转换为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;
	}
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kangwq2017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值