POJ 1051 P,MTHBGWB 简单字符串转换
2010-12-20 21:32
183 查看
http://poj.org/problem?id=1051
P,MTHBGWB
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:
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):
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
Sample Output
P,MTHBGWB
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5088 | Accepted: 2924 |
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 | ..- |
underscore | ..-- | period | ---. |
comma | .-.- | question mark | ---- |
.- -.-. -- ..-- --. .-. . .- - . .-. ..-- -. -.-- ..-- .-. . --. .. --- -.
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
/* Author : yan * Question : POJ 1051P,MTHBGWB * Data : Monday, December 20 2010 */ #include<stdio.h> #define MAX 102 char str[MAX]; char code[4*MAX]; int code_len[MAX]; int cnt; int read_cnt; int len; const char * dic[]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..","..--","---.",".-.-","----"}; int len_of_code(char ch) { if(ch-'A'<26&&ch-'A'>=0) return strlen(dic[ch-'A']); else return 4; } void addcode(char ch) { int _i; char cache[5]; if(ch-'A'>=0&&ch-'A'<26) strcpy(cache,dic[ch-'A']); else if(ch=='_') strcpy(cache,dic[26]); else if(ch=='.') strcpy(cache,dic[27]); else if(ch==',') strcpy(cache,dic[28]); else if(ch=='?') strcpy(cache,dic[29]); for(_i=0;cache[_i]!='/0';_i++) { code[cnt++]=cache[_i]; } } void readcode() { char cache[5]; int _cnt; int _i,_j; for(_i=len-1;_i>=0;_i--) { _cnt=0; for(_j=0;_j<code_len[_i];_j++) cache[_cnt++]=code[read_cnt++]; cache[_cnt]='/0'; for(_j=0;_j<30;_j++) if(strcmp(cache,dic[_j])==0) { if(_j<26) printf("%c",_j+'A'); else if(_j==26) printf("_"); else if(_j==27) printf("."); else if(_j==28) printf(","); else printf("?"); } } } int main() { int n; int i,j; //freopen("input","r",stdin); scanf("%d",&n); for(i=0;i<n;i++) { cnt=0;read_cnt=0; scanf("%s",str); len=strlen(str); for(j=0;j<len;j++) { code_len[j]=len_of_code(str[j]); addcode(str[j]); } printf("%d: ",i+1); readcode(); printf("/n"); } return 0; }
相关文章推荐
- POJ 1051 P,MTHBGWB 简单字符串转换
- POJ 1051 P,MTHBGWB 简单字符串转换
- 基础题训练(简单数学题、字符串)P,MTHBGWB POJ-1051
- C#实现字符串转换成字节数组的简单实现方法
- UVa 725 简单枚举+整数转换为字符串
- 字符串的简单包含问题,主要看看转换问题的思路
- C++Builder RAD Studio XE, UTF-8 String 转换为 char * 字符串的最简单方式, 常用于sqlite3开发
- poj3650---将一个字符串中的特定字符转换
- .NET 关于字符串的十六进制转换为十进制 十进制转换为十六进制字符串的简单方法
- SDUT 2364 || POJ 2934 Automatic Correction of Misspellings(简单字符串处理)
- python 基础-----数字,字符串,if while 循环 数据类型的转换简单介绍
- C++ 字符串融合 和 string 与 int 之间最简单的转换方法
- poj 3077 Rounders 【简单字符串处理】
- POJ-3080 Blue Jeans(简单字符串)
- POJ 2503(简单map,字符串的处理)
- javascript时间戳和日期字符串相互转换代码(超简单)
- 数字转字符串(string)扩充说明及简单写数字和字符串转换代码(正式比赛的itoa和atoi用不了)
- [绍棠_swift] swift的简单学习(字符串、4元组、类型转换及运算)
- POJ 3193 Cow Phrasebook(简单字符串的处理)
- poj 1504 Adding Reversed Numbers(简单字符串的处理)