POJ 1051 P,MTHBGWB
2015-05-18 13:54
253 查看
题目链接: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: 7509 | Accepted: 4318 |
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_R***EN,_NEVERMORE. 4: TO_BE_OR_NOT_TO_BE? 5: THE_QUICK_BROWN_FOX_JUMPS_OVER_THE_LAZY_DOG
题意:字符串处理,换码
有点难以处理呢
<span style="font-size:18px;"><strong>#include<cstdio> #include<iostream> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<queue> #include<map> #include<stack> #include<list> #include<set> #include<cmath> #define INF 0x7fffffff #define eps 1e-6 const double pi = acos(-1.0); #define mod 1000000007 #define N 10010 typedef long long LL; using namespace std; //map<string,string>word; //priority_queue<int>pq; //priority_queue<int,vector<int>,great<int>>pq; char m[30][5] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---", ".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..","..--",".-.-","---.","----"}; <span style="color:#ff0000;">//换码</span></strong></span>
<span style="font-size:18px;"><strong>int main() { int n,num[120],k,t,j,l,f; <span style="color:#ff0000;">//num为每个字符转变为符号的宽度</span> cin >> n; char temp[120],u[1000]; <span style="color:#ff0000;"> //temp为输入字符,u为转变后的符号码</span> for(t=1;t<=n;t++) { u[0] = '\0'; cin>>temp; int len=strlen(temp); for(k=0;k<len;k++) { if(isalpha(temp[k])) strcat(u,m[temp[k]-'A']),num[k]=strlen(m[temp[k]-'A']); <span style="color:#ff0000;"> //atrcat函数连接字符串 </span> else if(temp[k]=='_') strcat(u,m[26]),num[k]=4; else if(temp[k]==',') strcat(u,m[27]),num[k]=4; else if(temp[k]=='.') strcat(u,m[28]),num[k]=4; else if(temp[k]=='?') strcat(u,m[29]),num[k]=4; } cout<<t<<": "; for(k=len-1,j=0;k!=-1;k--) { for(l=0,temp[0]='\0'; l<num[k];l++,j++) temp[l]=u[j]; temp[l]='\0'; for(l=0,f=1;f && l<26; l++) { if(!strcmp(m[l],temp)) { putchar('A'+l); f=0; } } if(f && !strcmp(m[26] , temp)) putchar('_'),f=0; if(f && !strcmp(m[27] , temp)) putchar(','),f=0; if(f && !strcmp(m[28] , temp)) putchar('.'),f=0; if(f && !strcmp(m[29] , temp)) putchar('?'),f=0; } putchar('\n'); } return 0; }<span style="color:#ff0000;"> </span></strong></span>
相关文章推荐
- 基础题训练(简单数学题、字符串)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
- POJ 1051 P,MTHBGWB(map模拟)
- P,MTHBGWB - 1051
- 1051 P,MTHBGWB
- PKU_ACM_1051_ P,MTHBGWB
- PKU-1051 P,MTHBGWB (map的使用)
- 1051 P,MTHBGWB
- poj 1051 P,MTHBGWB(字符串,较麻烦)
- 北大poj Q3 P,MTHBGWB
- zoj1068 P,MTHBGWB