poj1051 P,MTHBGWB

P,MTHBGWB
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5848 Accepted: 3356

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
#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)
{
int i;
char oneCode[5];
int len = strlen(str);
strcpy(morseString,"");
for(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];
int i;
for(i=0;i<len;i++)
{
dott[i] = dots[len-i-1];
}
for(i=0;i<len;i++)
{
dots[i] = dott[i];
}
}

char getLetter(char* s)
{
int i;
for(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)
{
int i,j;
char ans[110];
int An = 0;
int count = 0;
char eachCode[5];
for(i=0;i<len;i++)
{
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)
{
scanf("%s",&str);
printf("%d: ",num+1);
analysis(morseString, str,dots);
reverse(dots, strlen(str));
decode(morseString, dots, strlen(str));
num++;
}
return 0;
}
 

转载于:https://www.cnblogs.com/w0w0/archive/2011/11/21/2256738.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值