Your task is to implement a decoder of the famous Morse alphabet. As most of you know,
the Morse code represents characters as variable-length sequences of short and long signals
(“beeps”), often written as dots and dashes. For those who do not remember their scouting
years, the following table shows the Morse code sequences for all letters:
A .- E . I .. M – Q –.- U ..- Y -.–
B -… F ..-. J .— N -. R .-. V …- Z –..
C -.-. G –. K -.- O — S … W .–
D -.. H …. L .-.. P .–. T - X -..-
If more letters are to be transferred, they are separated by a short pause, typically written as
a slash. A space between words is represented by an even longer pause, written as two slashes.
Input Specification
The input contains several test cases. Each test case is specified on one line with at most 1000
characters, which describes a valid Morse code transmission. Specifically:
• The line consists only of dashes (“-”), dots (“.”), and slashes (“/”).
• There is at least one character.
• The first and last characters will never be a slash.
• There will never be more than two slashes together.
• Each non-empty sequence between two slashes contains a valid Morse code of one letter.
Output Specification
For each test case, print one line containing the decoded message in uppercase letters.
Sample Input
.-/-.-./–
-.-./-/..-//—/.–././-.
./-/-././-/./.-./.-//-…/.-././…-/../-/-.–//-.-./…./.-/.-../.-.././-./–./.
Output for Sample Input
ACM
CTU OPEN
ETNETERA BREVITY CHALLENGE
题意:26个字母代表不同的符号,/代表一个不同的字母,//代表一个单词之间的空格。这个题可以设成二维数组,每一行只代表一个单词
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
char change[26][5] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
int main()
{
char s[1100], t[5];
int slen, i, j, k;
while(~scanf("%s", s))
{
k = 0;
slen = strlen(s);
for(i = 0; i < slen; i++)
{
if(s[i] == '/')
{
t[k] = '\0';
if(k == 0)
printf(" ");
else for(j = 0; j < 26; j++)
if(strcmp(change[j], t) == 0)
printf("%c", j+'A');
k = 0;
}
else
{
t[k++] = s[i];
}
}
if(k == 0)
printf(" ");
else for(j = 0; j < 26; j++)
if(strcmp(change[j], t) == 0)
printf("%c", j+'A');
printf("\n");
}
return 0;
}