新手,求帮助,程序崩溃,我是大一的 !!
题目Problem D: 一种基于摩尔斯电码密码
Time Limit: 1 Sec Memory Limit: 16 MB
Submit: 199 Solved: 94
[Submit][Status][Web Board]
Description
Morse code represents characters as variable length sequences of dots and dashes. In practice, characters in a message are delimited by short pauses. The following table shows the Morse code sequences:
A .- H .... O --- V ...-
B -... I .. P .--. W .--
C -.-. J .--- Q --.- X -..-
D -.. K -.- R .-. Y -.--
E . L .-.. S ... Z --..
F ..-. M -- T -
G --. N -. U ..-
Note that four dot-dash combinations are unassigned. For the purposes of this problem we will assign them as follows (these are not the assignments for actual Morse code):
underscore ..-- period ---.
comma .-.- question mark ----
Thus, the message "ACM_GREATER_NY_REGION" is encoded as:
.- -.-. -- ..-- --. .-. . .- - . .-. ..-- -. -.-- ..-- .-. . --. .. --- -.
M.E. Ohaver proposed an encryption scheme based on mutilating Morse code. Her scheme replaces the pauses between letters, necessary because Morse is a variable-length encoding that is not prefix-free, with a string that identifies the number of dots and dashes in each. For example, consider the message ".--.-.--". Without knowing where the pauses should be, this could be "ACM", "ANK", or several other possibilities. If we add length information, however, ".--.-.--242", then the code is unabiguous.
Ohaver's scheme has three steps, the same for encryption and decryption:
1. Convert the text to Morse code without pauses but with a string of numbers to indicate code lengths
2. Reverse the string of numbers
3. Convert the dots and dashes back into to text using the reversed string of numbers as code lengths
As an example, consider the encrypted message "AKADTOF_IBOETATUK_IJN". Converting to Morse code with a length string yields ".--.-.--..----..-...--..-...---.-.--..--.-..--...----.232313442431121334242". Reversing the numbers and decoding yields the original message "ACM_GREATER_NY_REGION".
Input
This problem requires that you implement Ohaver's encoding algorithm. The input will consist of several messages encoded with Ohaver's algorithm. The first line of the input is an integer n that specifies the number of test cases. The following n lines contain one message per line. Each message will use only the twenty-six capital letters, underscores, commas, periods, and question marks. Messages will not exceed 100 characters in length.
Output
For each message in the input, output the line number starting in column one, a colon, a space, and then the decoded message. The output format must be adhered to precisely.
Sample Input
5
AKADTOF_IBOETATUK_IJN
PUEL
QEWOISE.EIVCAEFNRXTBELYTGD.
?EJHUT.TSMYGW?EJHOT
DSU.XFNCJEVE.OE_UJDXNO_YHU?VIDWDHPDJIKXZT?E
Sample Output
1: ACM_GREATER_NY_REGION
2: PERL
3: QUOTH_THE_RAVEN,_NEVERMORE.
4: TO_BE_OR_NOT_TO_BE?
5: THE_QUICK_BROWN_FOX_JUMPS_OVER_THE_LAZY_DOG
[color=#ff0000]我的代码[/color]
# include <string.h>
# include <stdio.h>
# define MAX 150
struct type
{
char str [100];
char let;
int len;
} letter[30]= {{".-",'A',2},{"-...",'B',4},{"-.-.",'C',4},{"-..",'D',3},{".",'E',1},{"..-.",'F',4},{"--.",'G',3},{"....",'H',4},{"..",'I',2},{".---",'J',4}, {"-.-",'K',3},{".-..",'L',4},{"--",'M',2},{"-.",'N',2},{"---",'O',3},{".--.",'P',4},{"--.-",'Q',4},{".-.",'R',3},{"...",'S',3},{"-",'T',1},{"..-",'U',3},{"...-",'V',4},{".--",'W',3},{"-..-",'X',4},{"-.--",'Y',4},{"--..",'Z',4},{"..--",'_',4},{".-.-",',',4},{"---.",'.',4},{"----",'?',4}};
int main ()
{
int i,j,n,m,cha,v,z=0,y=0,s=0;
int lang [MAX];
char message [MAX];
char yiciyi [300];
char e[5];
scanf ("%d",&n);
for ( i = 0 ; i < n ; i ++)
{
scanf ("%s",message);
for ( j = 0 ; message [j] != '\0' ; j++ )
{
for ( cha = 1 ; cha <= 30 ; cha++ )
{
if ( message [j]== letter[cha].let)
{
y = j;
lang[j]=letter[cha].len;
strcat(yiciyi,letter[cha].str);
break;
}
}
}
for ( j = y ; j >= 0 ; j-- )
{
m = lang [j];
for ( v = z; v < m + z; v++ )
{
e[s++]=yiciyi[v];
}
for ( cha = 1 ; cha <= 30 ; cha++ )
{
if ( e == letter[cha].str)
{
printf ("%c",letter[cha].let);
}
memset(e,0,100);
}
z = m + 1 ;
s = 0;
}
}
}
[ 本帖最后由 amorvos 于 2011-12-28 16:27 编辑 ]