1、http://poj.org/problem?id=10512、题目:P,MTHBGWBTime Limit: 1000MS Memory Limit: 10000K Total Submissions: 6798 Accepted: 3911 DescriptionMorse 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". InputThis 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.OutputFor 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 Input5AKADTOF_IBOETATUK_IJNPUELQEWOISE.EIVCAEFNRXTBELYTGD.?EJHUT.TSMYGW?EJHOTDSU.XFNCJEVE.OE_UJDXNO_YHU?VIDWDHPDJIKXZT?ESample Output1: 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_DOG3、代码:#include#include#includeusing namespace std;char str[1110];string rstr;int rnum[1110];char change[26][10]= {".-","-...","-.-.","-..",".","..-.","--.","....","..", ".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-", ".--","-..-","-.--","--.." };char change2[4][10]= {"..--","---.",".-.-","----"};char find1(char c[]){ for(int i=0; i<26 i="" if="" strcmp="" c="" change="" i="" 0="" return="" a="" i="" char="" find2="" char="" c="" if="" strcmp="" c="" --="" 0="" return="" _="" else="" if="" strcmp="" c="" ---="" 0="" return="" else="" if="" strcmp="" c="" -="" -="" 0="" return="" else="" if="" strcmp="" c="" ----="" 0="" return="" int="" find3="" char="" c="" if="" c="='_')" return="" 0="" else="" if="" c="='.')" return="" 1="" else="" if="" c="=',')" return="" 2="" else="" if="" c="='?')" return="" 3="" int="" main="" int="" t="" cas="0;" cin="">>t; while(t--) { cas++; cin>>str; char ans[1110]; int len=strlen(str); rstr=""; for(int i=0; i='A' && str[i]<='Z') { int tmp=str[i]-'A'; rstr+=change[tmp]; rnum[i]=strlen(change[tmp]); } else { int tmp=find3(str[i]); rstr+=change2[tmp]; rnum[i]=strlen(change2[tmp]); } } int aa=0; int l=rstr.size(); int p=len-1,pp=0; char temp[10]; memset(ans,0,sizeof(ans)); for(int i=0; i<l;) { memset(temp,0,sizeof(temp)); int k=0; for(int j=i; j<i+rnum[p]; j++) { temp[k++]=rstr[j]; } if(strcmp(temp,"..--")==0) ans[pp]='_'; else if(strcmp(temp,"---.")==0) ans[pp]='.'; else if(strcmp(temp,".-.-")==0) ans[pp]=','; else if(strcmp(temp,"----")==0) ans[pp]='?'; else ans[pp]=find1(temp); i+=rnum[p]; p--; pp++; } printf("%d: %s\n",cas,ans); } return 0;}/*5AKADTOF_IBOETATUK_IJNPUELQEWOISE.EIVCAEFNRXTBELYTGD.?EJHUT.TSMYGW?EJHOTDSU.XFNCJEVE.OE_UJDXNO_YHU?VIDWDHPDJIKXZT?E*/
详细解决方案
相关解决方案
- 1051. 复数乘法 (15) PAT
- PAT甲级-1051 Pop Sequence (25分)【推荐!!!】
- PAT乙级-1051 复数乘法 (15分)
- Java - PAT- 1051. 复数乘法 (15)
- LeetCode—— 1051 高度检查器
- HDU 1051 Wooden Sticks 【贪心】
- 【LeetCode】1051. 高度检查器 C++sort 排序的使用
- 1051: 奥运会跳水比赛
- 2021秋季《数据结构》_EOJ 1051.插入排序
- 51nod 1051 最大子矩阵和 2019/03/05
- Leetcode 1051. Height Checker
- LeetCode-1051:高度检查器
- Python - Django 执行 migrate 操作时异常: django.db.utils.OperationalError: (1051, “Unknown table ‘xxx‘“)
- 【高度检查器(1051-java)】
- PAT (Basic Level) Practice 1051 复数乘法
- Wooden Sticks HDU 1051
- PAT乙级 1046 划拳 (15分) 1051 复数乘法 (15分)
- hihoCoder 1051 补提交卡(贪心枚举)
- 1051 最大矩阵和
- pat--1051 Pop Sequence(25 分)(stack出栈入栈)
- Dp - 51NOD - 1051 最大子矩阵和
- hihoCoder #1051 : 补提交卡
- poj 1051 P,MTHBGWB(字符串,较麻烦)
- poj 1051 P,MTHBGWB (模拟题字符串 )
- 51Nod 1051 最大子矩阵和 (最大子段和+前缀和)
- 力扣 <数组> 1051. 高度检查器 简单
- 【PAT乙级】1051 复数乘法