基础题训练(简单数学题、字符串)P,MTHBGWB POJ-1051
来源:互联网 发布:原油看盘软件 编辑:程序博客网 时间:2024/06/05 15:32
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".
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
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
#include <iostream>#include <cstring>using namespace std;int main(){ char l[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZ_.,?"}, *m[]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..","..--","---.",".-.-","----"}, s[105]={},temp[500]={}; int a[105]={},T,NO=1; cin>>T; while(T--) { cin>>s; cout<<NO++<<": "; int len=strlen(s),count=0; for(int i=0;i<len;i++) //从s中往l中遍历找到后往m遍历找到对应的码放到temp中组成新的码 { for(int j=0;j<strlen(l);j++) { if(s[i]==l[j]) { strcat(temp,m[j]); a[i]=strlen(m[j]); count++; break; } } } char *p=temp; for(int i=count-1;i>=0;i--) { for(int j=0;j<a[i];j++) { s[j]=*(p++); s[j+1]=0; } for(int k=0;k<31;k++) { if(strcmp(m[k],s)==0) { cout<<l[k]; break; } } } cout<<endl; memset(temp,0,sizeof(temp)); }}
0 0
- 基础题训练(简单数学题、字符串)P,MTHBGWB POJ-1051
- POJ 1051 P,MTHBGWB 简单字符串转换
- POJ 1051 P,MTHBGWB 简单字符串转换
- poj 1051 P,MTHBGWB(字符串,较麻烦)
- POJ 1051 P,MTHBGWB
- POJ-1051-P,MTHBGWB
- POJ 1051: P,MTHBGWB
- POJ 1051 P,MTHBGWB
- poj 1051 P,MTHBGWB
- POJ 1051 P,MTHBGWB
- POJ 1051 P,MTHBGWB(map模拟)
- POJ 1051 P,MTHBGWB 笔记
- poj 1051 P,MTHBGWB (模拟题字符串 )
- POJ 1051 P,MTHBGWB 已被翻译
- 1051 P,MTHBGWB
- P,MTHBGWB - 1051
- 1051 P,MTHBGWB
- 北大poj Q3 P,MTHBGWB
- hdu-1216-Assistance Required
- 学期总结
- 06 为什么会有继承?为什么会有super?
- 千万别用MongoDB?
- libevent代码例子---- echo server
- 基础题训练(简单数学题、字符串)P,MTHBGWB POJ-1051
- 如何写出高效C++(模板与泛型编程)
- HDU 1231 最大连续子序列【dp】
- jQuery中Ajax事件beforesend及各参数含义
- Remove Nth Node From End of List
- 深度学习caffe平台--train_val.prototxt文件中激活层(Activiation Layers)及参数及参数详解
- HDU 5755 Gambler Bo(高斯消元)
- Android使用Aidl实现跨进程通信
- Java多线程中的volatile