P,MTHBGWB

 FJNU.1074
PKU.1051

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:
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".

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

Source
Greater New York 2001

My Program

#include < iostream >
#include
< map >
#include
< string >
#include
< vector >
using   namespace  std;

int  main()
{
    map
<char,string>letter;
    map
<string,char>code;
    
string str,txt,word;
    vector
<int> key;
    
int n,m,i,j,k,s,t;
    letter[
'A']=".-";        letter['H']="....";        letter['O']="---";        letter['V']="...-";
    letter[
'B']="-...";        letter['I']="..";        letter['P']=".--.";        letter['W']=".--";
    letter[
'C']="-.-.";        letter['J']=".---";        letter['Q']="--.-";        letter['X']="-..-";
    letter[
'D']="-..";        letter['K']="-.-";        letter['R']=".-.";        letter['Y']="-.--";
    letter[
'E']=".";        letter['L']=".-..";        letter['S']="...";        letter['Z']="--..";
    letter[
'F']="..-.";        letter['M']="--";        letter['T']="-";
    letter[
'G']="--.";        letter['N']="-.";        letter['U']="..-";
    letter[
'_']="..--";        letter[',']=".-.-";        letter['.']="---.";        letter['?']="----";
    code[
".-"]='A';            code["...."]='H';        code["---"]='O';        code["...-"]='V';
    code[
"-..."]='B';        code[".."]='I';            code[".--."]='P';        code[".--"]='W';
    code[
"-.-."]='C';        code[".---"]='J';        code["--.-"]='Q';        code["-..-"]='X';
    code[
"-.."]='D';        code["-.-"]='K';        code[".-."]='R';        code["-.--"]='Y';
    code[
"."]='E';            code[".-.."]='L';        code["..."]='S';        code["--.."]='Z';
    code[
"..-."]='F';        code["--"]='M';            code["-"]='T';
    code[
"--."]='G';        code["-."]='N';            code["..-"]='U';
    code[
"..--"]='_';        code[".-.-"]=',';        code["---."]='.';        code["----"]='?';

    cin
>>n;
    
for(t=1;t<=n;t++)
    
{
        cin
>>str;
        m
=str.size();
        txt.erase(txt.begin(),txt.end());
        key.erase(key.begin(),key.end());
        
for(i=0;i<m;i++)
        
{
            key.push_back(letter[(str[i])].size());
            txt
+=letter[(str[i])];
        }

        s
=0;
        
for(i=0;i<m/2;i++)
            swap(key[i],key[m
-i-1]);
        cout
<<t<<"";
        
for(i=0;i<m;i++)
        
{
            k
=key[i];
            word.erase(word.begin(),word.end());
            
for(j=s;j<s+k;j++)
                word
+=txt[j];
            cout
<<code[word];
            s
+=k;
        }

        cout
<<endl;
    }

    
return 0;
}

YOYO's Note:
┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄它是华丽的分隔线

【题意简述】

Morse密码加密字符串的方法是:将串中的每一个字母转换成相应的密码并统计该密码的长度,接着将密码长度序列反转,输出相应长度的密码。现在有n个Morse密码加密过的密文,把它翻译成明文。


【粗略分析】

字符串处理、模拟题。题目中已经把转换规则说的很清楚了。
我们只要把字符串一一输入并转换为代码密文,同时统计该长度,
接着倒置密码长度序列,再根据代码密文找出相应的字符并输出结果就可以了。
为了方便,这里用两个MAP分别用来将字符转换为代码及反转。
p.s 现在写的时候发现说其实不用放进str,直接插入代码密文就好了,
但反正这题数据比较弱,0.00sAC,就不管这么多了 = = ……


【C++源代码】

#include < iostream >
#include
< map >
#include
< string >
#include
< vector >
using   namespace  std;

int  main()
{
    map
<char,string>letter;
    map
<string,char>code;
    
string str,txt,word;
    vector
<int> key;
    
int n,m,i,j,k,s,t;
    letter[
'A']=".-";        letter['H']="....";        letter['O']="---";        letter['V']="...-";
    letter[
'B']="-...";        letter['I']="..";        letter['P']=".--.";        letter['W']=".--";
    letter[
'C']="-.-.";        letter['J']=".---";        letter['Q']="--.-";        letter['X']="-..-";
    letter[
'D']="-..";        letter['K']="-.-";        letter['R']=".-.";        letter['Y']="-.--";
    letter[
'E']=".";        letter['L']=".-..";        letter['S']="...";        letter['Z']="--..";
    letter[
'F']="..-.";        letter['M']="--";        letter['T']="-";
    letter[
'G']="--.";        letter['N']="-.";        letter['U']="..-";
    letter[
'_']="..--";        letter[',']=".-.-";        letter['.']="---.";        letter['?']="----";
    code[
".-"]='A';            code["...."]='H';        code["---"]='O';        code["...-"]='V';
    code[
"-..."]='B';        code[".."]='I';            code[".--."]='P';        code[".--"]='W';
    code[
"-.-."]='C';        code[".---"]='J';        code["--.-"]='Q';        code["-..-"]='X';
    code[
"-.."]='D';        code["-.-"]='K';        code[".-."]='R';        code["-.--"]='Y';
    code[
"."]='E';            code[".-.."]='L';        code["..."]='S';        code["--.."]='Z';
    code[
"..-."]='F';        code["--"]='M';            code["-"]='T';
    code[
"--."]='G';        code["-."]='N';            code["..-"]='U';
    code[
"..--"]='_';        code[".-.-"]=',';        code["---."]='.';        code["----"]='?';

    cin
>>n;
    
for(t=1;t<=n;t++)
    
{
        cin
>>str;
        m
=str.size();
        txt.erase(txt.begin(),txt.end());
        key.erase(key.begin(),key.end());
        
for(i=0;i<m;i++)
        
{
            key.push_back(letter[(str[i])].size());
            txt
+=letter[(str[i])];
        }

        s
=0;
        
for(i=0;i<m/2;i++)
            swap(key[i],key[m
-i-1]);
        cout
<<t<<"";
        
for(i=0;i<m;i++)
        
{
            k
=key[i];
            word.erase(word.begin(),word.end());
            
for(j=s;j<s+k;j++)
                word
+=txt[j];
            cout
<<code[word];
            s
+=k;
        }

        cout
<<endl;
    }

    
return 0;
}

【注意事项】

※ 每次转换完一个数、一个串时记得把对应的容器清空。


【点评】

 字符串处理,用STL增的很方便 = = ……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值