zoj1068 P,MTHBGWB

 题目是关于编码方面的,使用Morse code来编码,而解码的方式却不同,我们首先记录在编码时每个字母的码长,然后将整个码长序列倒过来,然后通过这个编码方式来解码。文章就是需要我们用这个方式把那些加密的文字解密出来。

所以方法就是照着他所讲的一步步来。首先把文字编码,同时记录码长,然后reverse,然后解码。没一部都没什么难度,只要小心就行。这道题目就是麻烦,光是把Morse code 敲进去就费了好大力。代码如下:

#include<stdio.h> #include<string.h> struct code { char c; char cc[5]; }morseCode[50]; void initMorseCode() { morseCode[0].c = 'A'; strcpy(morseCode[0].cc,".-"); morseCode[1].c = 'B'; strcpy(morseCode[1].cc,"-..."); morseCode[2].c = 'C'; strcpy(morseCode[2].cc,"-.-."); morseCode[3].c = 'D'; strcpy(morseCode[3].cc,"-.."); morseCode[4].c = 'E'; strcpy(morseCode[4].cc,"."); morseCode[5].c = 'F'; strcpy(morseCode[5].cc,"..-."); morseCode[6].c = 'G'; strcpy(morseCode[6].cc,"--."); morseCode[7].c = 'H'; strcpy(morseCode[7].cc,"...."); morseCode[8].c = 'I'; strcpy(morseCode[8].cc,".."); morseCode[9].c = 'J'; strcpy(morseCode[9].cc,".---"); morseCode[10].c = 'K'; strcpy(morseCode[10].cc,"-.-"); morseCode[11].c = 'L'; strcpy(morseCode[11].cc,".-.."); morseCode[12].c = 'M'; strcpy(morseCode[12].cc,"--"); morseCode[13].c = 'N'; strcpy(morseCode[13].cc,"-."); morseCode[14].c = 'O'; strcpy(morseCode[14].cc,"---"); morseCode[15].c = 'P'; strcpy(morseCode[15].cc,".--."); morseCode[16].c = 'Q'; strcpy(morseCode[16].cc,"--.-"); morseCode[17].c = 'R'; strcpy(morseCode[17].cc,".-."); morseCode[18].c = 'S'; strcpy(morseCode[18].cc,"..."); morseCode[19].c = 'T'; strcpy(morseCode[19].cc,"-"); morseCode[20].c = 'U'; strcpy(morseCode[20].cc,"..-"); morseCode[21].c = 'V'; strcpy(morseCode[21].cc,"...-"); morseCode[22].c = 'W'; strcpy(morseCode[22].cc,".--"); morseCode[23].c = 'X'; strcpy(morseCode[23].cc,"-..-"); morseCode[24].c = 'Y'; strcpy(morseCode[24].cc,"-.--"); morseCode[25].c = 'Z'; strcpy(morseCode[25].cc,"--.."); morseCode[26].c = '_'; strcpy(morseCode[26].cc,"..--"); morseCode[27].c = ','; strcpy(morseCode[27].cc,".-.-"); morseCode[28].c = '.'; strcpy(morseCode[28].cc,"---."); morseCode[29].c = '?'; strcpy(morseCode[29].cc,"----"); } void getCode(char* oneCode, char c) { if(c>='A'&&c<='Z') { strcpy(oneCode, morseCode[c-'A'].cc); return; } else { for(int i=26;i<30;i++) { if(c==morseCode[i].c) { strcpy(oneCode, morseCode[i].cc); return; } } } return; } void analysis(char* morseString, char* str, int* dots) { char oneCode[5]; int len = strlen(str); strcpy(morseString,""); for(int i=0;i<len;i++) { getCode(oneCode,str[i]); strcat(morseString,oneCode); dots[i] = strlen(oneCode); } return; } void reverse(int* dots, int len) { int dott[110]; for(int i=0;i<len;i++) { dott[i] = dots[len-i-1]; } for(int i=0;i<len;i++) { dots[i] = dott[i]; } } char getLetter(char* s) { for(int i=0;i<30;i++) { if(strcmp(morseCode[i].cc,s)==0) return morseCode[i].c; } return ' '; } void decode(char* morseString, int* dots, int len) { char ans[110]; int An = 0; int count = 0; char eachCode[5]; for(int i=0;i<len;i++) { int j; for(j=0;j<dots[i];j++) { eachCode[j] = morseString[count]; count++; } eachCode[j] = '/0'; ans[An++] = getLetter(eachCode); } ans[An] = '/0'; printf("%s/n",ans); } int main() { initMorseCode(); int N; char str[110]; int dots[110]; char morseString[500]; scanf("%d",&N); int num = 0; while(num!=N) { printf("%d: ",num+1); scanf("%s",&str); analysis(morseString, str,dots); reverse(dots, strlen(str)); decode(morseString, dots, strlen(str)); num++; } }

你可能感兴趣的:(c,加密,解密)