PKU-1051 P,MTHBGWB (map的使用)
来源:互联网 发布:mac yosemite dmg下载 编辑:程序博客网 时间:2024/04/27 23:11
原题链接 http://poj.org/problem?id=1051
P,MTHBGWB
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 6854 Accepted: 3942
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~Z
//A~Z
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
//4个符号(_ , . ?)
"..--", ".-.-", "---.", "----"
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".
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
5AKADTOF_IBOETATUK_IJNPUELQEWOISE.EIVCAEFNRXTBELYTGD.?EJHUT.TSMYGW?EJHOTDSU.XFNCJEVE.OE_UJDXNO_YHU?VIDWDHPDJIKXZT?E
Sample Output
1: ACM_GREATER_NY_REGION2: PERL3: QUOTH_THE_RAVEN,_NEVERMORE.4: TO_BE_OR_NOT_TO_BE?5: THE_QUICK_BROWN_FOX_JUMPS_OVER_THE_LAZY_DOG
Source Code
/*map的使用解密过程:1.输入解析成对应字符串以及相应数字长度2.反转数字长试串3.按反转的数字串进解析的字符串进行反解析*/#include <iostream>#include <map>#include <string>using namespace std;//A~Z以及4个符号(_ , . ?) string dictionary[30] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "..--", ".-.-", "---.", "----"}; int main() {char ch;int i, j, k, cases;string input, output, temp;int numbers[101];map<string, int> index;for (i = 0; i < 30; i++) {index.insert(make_pair(dictionary[i], i));}while (scanf("%d", &cases) !=EOF) {for (j = 1; j <= cases; j++) {output.erase();cin >> input;for (i = 0; i < input.length(); i++) {ch = input.at(i);if (ch >= 'A' && ch <= 'Z') {temp = dictionary[ch - 'A'];output += temp;numbers[i] = temp.length();} else {numbers[i] = 4;if (ch == '_') {output += dictionary[26];} else if (ch == ',') {output += dictionary[27];} else if (ch == '.') {output += dictionary[28];} else if (ch == '?') {output += dictionary[29];} }}int start = 0;printf("%d: ", j);for (i = input.length() - 1; i >= 0; i--) {temp = output.substr(start, numbers[i]);start = start + numbers[i];k = index[temp];if (k == 26) {printf("_");} else if (k == 27) {printf(",");} else if (k == 28) {printf(".");} else if (k == 29) {printf("?");} else {printf("%c", 'A' + k);}}printf("\n");}}}
- PKU-1051 P,MTHBGWB (map的使用)
- POJ 1051 P,MTHBGWB(map模拟)
- 1051 P,MTHBGWB
- POJ 1051 P,MTHBGWB
- POJ-1051-P,MTHBGWB
- POJ 1051: P,MTHBGWB
- POJ 1051 P,MTHBGWB
- P,MTHBGWB - 1051
- 1051 P,MTHBGWB
- poj 1051 P,MTHBGWB
- POJ 1051 P,MTHBGWB
- POJ 1051 P,MTHBGWB 笔记
- P,MTHBGWB
- P,MTHBGWB
- POJ 1051 P,MTHBGWB 简单字符串转换
- POJ 1051 P,MTHBGWB 简单字符串转换
- POJ 1051 P,MTHBGWB 已被翻译
- zoj1068 P,MTHBGWB
- 一张图片实现滚动
- 第三届蓝桥杯Java高职组决赛第一题
- Qt里对QLineEdit输入框进行判断
- 使用C++处理JSON数据交换格式
- SQL2012的一些新函数
- PKU-1051 P,MTHBGWB (map的使用)
- KMP算法详解——适合初学KMP算法的朋友
- 揭秘中国16大暴利行业
- could not fetch initial value for increment gener
- Android---简单的摄像程序
- 设计业务时要兼顾实现方案的可行性和方案的可操作性
- 黑马程序员--java字符串
- IOS开发中的数据存储方式
- 干系人的管理