poj1051 细心模拟
2016-05-16 19:57
441 查看
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
题意:
看题目中的例子就可以看懂:先把对应的单词转化为字符数组(句点或者横线),并且得到每一个字母转化得到字符的个数的数组,然后把数组倒置,按照这个数组转化为原来的文字(例如数组的第一个元素是 2 ,那么就是表示第一个字母是与字符数组前面两个字符对应(按照题目给的一个字符和字母的转化标准))
题解思路:
就是模拟上叙所说
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7841 | Accepted: 4485 |
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
题意:
看题目中的例子就可以看懂:先把对应的单词转化为字符数组(句点或者横线),并且得到每一个字母转化得到字符的个数的数组,然后把数组倒置,按照这个数组转化为原来的文字(例如数组的第一个元素是 2 ,那么就是表示第一个字母是与字符数组前面两个字符对应(按照题目给的一个字符和字母的转化标准))
题解思路:
就是模拟上叙所说
#include<stdio.h> #include<string.h> #include<string> #include<algorithm> using namespace std; struct Letter { char letter; string code; int len; }a[35]; bool cmp(string str1,string str2) { int len1=str1.length(); int len2=str2.length(); int flag=0; if(len1!=len2) return 1; for(int i=0;i<len1;i++){ if(str1[i]==str2[i]) continue; else{ flag=1; break; } } if(flag) return 1; else return 0; } int coun=1; int main() { a[0].len=2;a[0].letter='A';a[0].code=".-"; a[1].len=4;a[1].letter='B';a[1].code="-..."; a[2].len=4;a[2].letter='C';a[2].code="-.-."; a[3].len=3;a[3].letter='D';a[3].code="-.."; a[4].len=1;a[4].letter='E';a[4].code="."; a[5].len=4;a[5].letter='F';a[5].code="..-."; a[6].len=3;a[6].letter='G';a[6].code="--."; a[7].len=4;a[7].letter='H';a[7].code="...."; a[8].len=2;a[8].letter='I';a[8].code=".."; a[9].len=4;a[9].letter='J';a[9].code=".---"; a[10].len=3;a[10].letter='K';a[10].code="-.-"; a[11].len=4;a[11].letter='L';a[11].code=".-.."; a[12].len=2;a[12].letter='M';a[12].code="--"; a[13].len=2;a[13].letter='N';a[13].code="-."; a[14].len=3;a[14].letter='O';a[14].code="---"; a[15].len=4;a[15].letter='P';a[15].code=".--."; a[16].len=4;a[16].letter='Q';a[16].code="--.-"; a[17].len=3;a[17].letter='R';a[17].code=".-."; a[18].len=3;a[18].letter='S';a[18].code="..."; a[19].len=1;a[19].letter='T';a[19].code="-"; a[20].len=3;a[20].letter='U';a[20].code="..-"; a[21].len=4;a[21].letter='V';a[21].code="...-"; a[22].len=3;a[22].letter='W';a[22].code=".--"; a[23].len=4;a[23].letter='X';a[23].code="-..-"; a[24].len=4;a[24].letter='Y';a[24].code="-.--"; a[25].len=4;a[25].letter='Z';a[25].code="--.."; a[26].len=4;a[26].letter='_';a[26].code="..--"; a[27].len=4;a[27].letter=',';a[27].code=".-.-"; a[28].len=4;a[28].letter='.';a[28].code="---."; a[29].len=4;a[29].letter='?';a[29].code="----"; int n; char str[105],temp[410]; int num[105]; scanf("%d",&n); //getchar(); while(n--) { int pos=0; scanf("%s",str); printf("%d: ",coun++); int len_str=strlen(str); int t; for(int i=0;i<len_str;i++){ if(str[i]=='_') t=26; else if(str[i]==',') t=27; else if(str[i]=='.') t=28; else if(str[i]=='?') t=29; else t=str[i]-'A'; for(int j=0;j<a[t].len;j++) temp[pos++]=a[t].code[j]; num[i]=a[t].len; } reverse(num,num+len_str); int p=0; //temp[]的指针 for(int i=0;i<len_str;i++){ char str_temp[10]; memset(str_temp,'\0',sizeof(str_temp)); t=0; //str_temp[]的指针 for(int j=0;j<num[i];j++) str_temp[t++]=temp[p++]; for(int j=0;j<30;j++){ if(cmp(str_temp,a[j].code)==0){ printf("%c",a[j].letter); break; } } } puts(""); } return 0; }
相关文章推荐
- Effective JavaScript :第一章
- fastclick 解决js穿透问题
- keepalived基于nginx实现双主模型
- ubuntu14.04 networking restart
- [置顶] 用模板统一对多线程变量的使用
- Android学习笔记6:使用Intent1
- 开源硬件pk
- 命名规范
- Android Handler消息机制简述
- 写给想成为前端工程师的同学们
- Android 热修复,没你想的那么难
- The Same Color
- JAVA——静态导入
- 团体程序设计天梯赛-练习集 L2-005 集合相似度 STL set集合的交和并
- 数据结构实验之链表四:有序链表的归并
- oracle查询操作和视图操作
- openssl生成加密密钥
- 不知名的bug,更新中...
- MySQL 5.5.49 大内存优化配置文件优化详解
- CSS 3 选择器的优先级