ZZNUOJ-2022 摩斯密码

本文介绍了一种摩斯密码解密算法的实现方法。该算法通过预设的摩斯密码对照表,将输入的摩斯密码串转换为对应的英文字符和数字。文章详细展示了如何解析摩斯密码,并提供了完整的C语言代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2022 : 摩斯密码

时间限制:1 Sec内存限制:128 MiB
提交:203答案正确:83

提交状态讨论区

题目描述

 

题目描述:

 

摩尔斯电码(又译为摩斯密码,Morse code)是一种时通时断的信号代码,通过不同的排列顺序来表达不同的英文字母、数字和标点符号。为了简单问题,去除所有的标点符号。

 

现在给你一段摩斯密码,请你把它们进行解密

 

 

输入

 

含有T(T<=20)组数据,每组数据包含一段摩斯密码(由字符 '.' 和 '-' 以及 空格组成,连续空格个数不超过两个),每段占一行(不超过1000个字符),现在请你对他们进行解密;

 

 

输出

 

输出对应的解密后的结果(所有解密结果如果是英文字母一律输出小写英文字母),保证结果仅包含 数字 空格 字母。

 

 

样例输入

复制

5
..
. .
. ..
.  ..
..  .-.. --- ...- .  -.-- --- ..-

样例输出

复制

 

i

ee

ei

e i

i love you

 

  • AC Code
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

struct Data
{
	char ch[10];
	char c;
};
int main()
{
	Data st[40]; 
	strcpy(st[1].ch,".-");
	st[1].c = 'a'; 
	
	strcpy(st[2].ch,"-...");
	st[2].c = 'b'; 

	strcpy(st[3].ch,"-.-.");
	st[3].c = 'c'; 
	
	strcpy(st[4].ch,"-..");
	st[4].c = 'd'; 
	
	strcpy(st[5].ch,".");
	st[5].c = 'e'; 
	
	strcpy(st[6].ch,"..-.");
	st[6].c = 'f'; 

	strcpy(st[7].ch,"--.");
	st[7].c = 'g'; 
	
	strcpy(st[8].ch,"....");
	st[8].c = 'h'; 
	
	strcpy(st[9].ch,"..");
	st[9].c = 'i';
	
	strcpy(st[10].ch,".---");
	st[10].c = 'j';  
	
	strcpy(st[11].ch,"-.-");
	st[11].c = 'k'; 
	
	strcpy(st[12].ch,".-..");
	st[12].c = 'l';
	
	strcpy(st[13].ch,"--");
	st[13].c = 'm';
	
	strcpy(st[14].ch,"-.");
	st[14].c = 'n';
	
	strcpy(st[15].ch,"---");
	st[15].c = 'o';

	strcpy(st[16].ch,".--.");
	st[16].c = 'p';
	
	strcpy(st[17].ch,"--.-");
	st[17].c = 'q';
	
	strcpy(st[18].ch,".-.");
	st[18].c = 'r';

	strcpy(st[19].ch,"...");
	st[19].c = 's';	
	
	strcpy(st[20].ch,"-");
	st[20].c = 't';
	
	strcpy(st[21].ch,"..-");
	st[21].c = 'u';
	
	strcpy(st[22].ch,"...-");
	st[22].c = 'v';
	
	strcpy(st[23].ch,".--");
	st[23].c = 'w';
	
	strcpy(st[24].ch,"-..-");
	st[24].c = 'x';
	
	strcpy(st[25].ch,"-.--");
	st[25].c = 'y';
	
	strcpy(st[26].ch,"--..");
	st[26].c = 'z';

	strcpy(st[27].ch,"-----");
	st[27].c = '0';
	
	strcpy(st[28].ch,".----");
	st[28].c = '1';

	strcpy(st[29].ch,"..---");
	st[29].c = '2';
	
	strcpy(st[30].ch,"...--");
	st[30].c = '3';
	
	strcpy(st[31].ch,"....-");
	st[31].c = '4';
	
	strcpy(st[32].ch,".....");
	st[32].c = '5';
	
	strcpy(st[33].ch,"-....");
	st[33].c = '6';
	
	strcpy(st[34].ch,"--...");
	st[34].c = '7';
	
	strcpy(st[35].ch,"---..");
	st[35].c = '8';
	
	strcpy(st[36].ch,"----.");
	st[36].c = '9';
	
	int T;
	scanf("%d",&T);
	char ch2;
	ch2 = getchar();
	while(T--)
	{
		char str1[1005];
		char str2[1005];
		gets(str1);
		memset(str2,0,sizeof(str2));
		char cc[10];
		int  x=-1;
		int t = strlen(str1);
		int begin=0, end=0;         //下标 
		for(int i=0; i<t; i++)
		{
			if(i==0) begin = 0;
			if(str1[i] != ' ') 
			{
				end = i;
			}
			if( (str1[i] == ' ') || end == t-1 )
			{
				if(str1[i] == ' ') end = i-1;
				memset(cc,0,sizeof(cc));
				strncpy(cc, str1+begin, end-begin+1); //截取字符串存入新的字符串中 
		//		printf("%s\n",cc);
//				printf("%d\n",strlen(cc));
				int j;
				for(j=1; j<=36; j++)
				{
					if(strcmp(st[j].ch,cc) == 0)
					{
						str2[++x] = st[j].c;
						break;
					} 
				}
				begin = end + 2;
				memset(cc,0,sizeof(cc));
				if(str1[i+1] == ' ')	
				{
					str2[++x] = ' ';
					i=i+1;
					begin = begin + 1;
				}
						
			}
			
		} 
		printf("%s\n",str2);
		
	}
	
	return 0;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值