1068 P,MTHBGWB

来源:互联网 发布:深圳资深淘宝 编辑:程序博客网 时间:2024/05/16 08:25

P,MTHBGWB

Time limit: 1 Seconds   Memory limit: 32768K  
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

 

#include <iostream>
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] 
= '
}
void reverse()
{
 int i, list[1000], j;
 j = len-1;
 for ( i = 0; i < len; i++ )
 {
  list[i] = code[j];
  j--;
 }
 for ( i = 0; i < len; i++ )
 {
  code[i] = list[i];
 }
}
void encrypt()
{
 int i, k, m, n;
 bool match;
 char word[5];
 n = 0;
 for ( i = 0; i < len; i++ )
 {
  for ( k = 0; k < code[i]; k++ )
  {
   word[k] = morse[n];
   n++;
  }
  word[code[i]] = '/0';
  for ( k = 0; k < 30; k++ )
  {
   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';
}