1068 P,MTHBGWB
来源:互联网 发布:深圳资深淘宝 编辑:程序博客网 时间:2024/05/16 08:25
Total Submit: 1297 Accepted Submit: 821
Problem
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”.
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.
Example
Input
5
AKADTOF_IBOETATUK_IJN
PUEL
QEWOISE.EIVCAEFNRXTBELYTGD.
?EJHUT.TSMYGW?EJHOT
DSU.XFNCJEVE.OE_UJDXNO_YHU?VIDWDHPDJIKXZT?E
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
Notes
As presented, this encryption scheme is only trivially secure. In fact it offers no security at all if the algorithm is known to the attacker. The key is the string of numbers needed to decide where the pauses should be inserted to recover the message, but with the method shown here, this information is encoded in and easily recovered from the encrypted data. Even should some other method be chosen to scramble the length information in the encoding, secrecy of the algorithm is the real key in this technique. Modifications of Ohaver’s technique do exist in which the security is not based on the secrecy of the algorithm.
http://acm.zju.edu.cn/show_problem.php?pid=1068
using namespace std;
char ciphertext[1000], plaintext[1000], morse[1000];
char letter[30][5] = {"..--", ".-", "-...", "-.-.", "-..", ".",
"..-.", "--.", "....", "..", ".---", "-.-",
".-..", "--", "-.", "---", ".--.", "--.-",
".-.", "...", "-", "..-", "...-", ".--",
"-..-", "-.--", "--..", "---.", ".-.-", "----"};
int code[1000], len;
void read();
void convert();
void reverse();
void encrypt();
int main()
{
int n, i;
cin >> n;
getchar();
for ( i = 1; i <= n; i++ )
{
read();
convert();
reverse();
encrypt();
cout << i << ": " << plaintext << endl;
}
return 0;
}
void read()
{
gets(ciphertext);
}
void convert()
{
len = strlen(ciphertext);
int i, j, k;
j = 0;
for ( i = 0; i < len; i++ )
{
if ( ciphertext[i] == '.' )
{
code[i] = strlen(letter[27]);
for ( k = 0; k < code[i]; k++ )
{
morse[j] = letter[27][k];
j++;
}
}
else if ( ciphertext[i] == ',' )
{
code[i] = strlen(letter[28]);
for ( k = 0; k < code[i]; k++ )
{
morse[j] = letter[28][k];
j++;
}
}
else if ( ciphertext[i] == '?' )
{
code[i] = strlen(letter[29]);
for ( k = 0; k < code[i]; k++ )
{
morse[j] = letter[29][k];
j++;
}
}
else if ( ciphertext[i] == '_' )
{
code[i] = strlen(letter[0]);
for ( k = 0; k < code[i]; k++ )
{
morse[j] = letter[0][k];
j++;
}
}
else
{
code[i] = strlen(letter[ciphertext[i]-'A'+1]);
for ( k = 0; k < code[i]; k++ )
{
morse[j] = letter[ciphertext[i]-'A'+1][k];
j++;
}
}
}
morse[j] = '
{
int i, list[1000], j;
for ( i = 0; i < len; i++ )
{
list[i] = code[j];
j--;
}
for ( i = 0; i < len; i++ )
{
code[i] = list[i];
}
}
{
int i, k, m, n;
bool match;
char word[5];
for ( i = 0; i < len; i++ )
{
for ( k = 0; k < code[i]; k++ )
{
word[k] = morse[n];
n++;
}
word[code[i]] = '/0';
{
if ( strlen(word) == strlen(letter[k]) )
{
match = true;
for ( m = 0; m < strlen(word); m++ )
{
if ( word[m] != letter[k][m] )
{
match = false;
break;
}
}
if ( !match )
continue;
else
break;
}
}
if ( k == 0 )
plaintext[i] = '_';
else if ( k == 27 )
plaintext[i] = '.';
else if ( k == 28 )
plaintext[i] = ',';
else if ( k == 29 )
plaintext[i] = '?';
else
plaintext[i] = char(k+'A'-1);
}
plaintext[len] = '/0';
}
- 1068 P,MTHBGWB
- ZOJ 1068 P,MTHBGWB
- 1068:P,MTHBGWB
- ZOJ 1068 P,MTHBGWB
- P,MTHBGWB
- P,MTHBGWB
- zoj1068 P,MTHBGWB
- 1051 P,MTHBGWB
- fjnu 1074 P,MTHBGWB
- PKU_ACM_1051_ P,MTHBGWB
- zoj1068 P,MTHBGWB
- ZOJ1068 P,MTHBGWB
- POJ 1051 P,MTHBGWB
- POJ-1051-P,MTHBGWB
- POJ 1051: P,MTHBGWB
- POJ 1051 P,MTHBGWB
- P,MTHBGWB - 1051
- 1051 P,MTHBGWB
- 1001 A + B Problem
- 1002 Fire Net
- 1006 Do the Untwist
- RoR的OO与敏捷[1][88250原创]
- 1057 Undercut
- 1068 P,MTHBGWB
- 37个我爱Ruby的理由
- JavaScript校验学习总结
- 常用正则表达式
- 小心陷阱--% of C
- jQuery简单体验及jQuery中文手册下载
- 瑞捷校园网认证软件(支持双网卡)
- 使用Hibernate轻松解决java中date与datetime类型不兼容的问题
- 百年语言 ZZ