其他语言

本类阅读TOP10

·基于Solaris 开发环境的整体构思
·使用AutoMake轻松生成Makefile
·BCB数据库图像保存技术
·GNU中的Makefile
·射频芯片nRF401天线设计的分析
·iframe 的自适应高度
·BCB之Socket通信
·软件企业如何实施CMM
·入门系列--OpenGL最简单的入门
·WIN95中日志钩子(JournalRecord Hook)的使用

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
浙大在线评测 1068 P,MTHBGWB

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

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.



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.


Solution:

// 声明:本代码仅供学习之用,请不要作为个人的成绩提交。
// http://blog.csdn.net/mskia
// email: [email protected]

#include <iostream>
#include <vector>
#include <iterator>
#include <map>
#include <string>

using namespace std;

int main( void ) {
    map<char, string> container;
    container['A'] = ".-";   container['H'] = "...."; container['O'] = "---";  container['V'] = "...-";
    container['B'] = "-..."; container['I'] = "..";   container['P'] = ".--."; container['W'] = ".--";
    container['C'] = "-.-."; container['J'] = ".---"; container['Q'] = "--.-"; container['X'] = "-..-";
    container['D'] = "-..";  container['K'] = "-.-";  container['R'] = ".-.";  container['Y'] = "-.--";
    container['E'] = ".";    container['L'] = ".-.."; container['S'] = "...";  container['Z'] = "--..";
    container['F'] = "..-."; container['M'] = "--";   container['T'] = "-"; container['G'] = "--.";
    container['N'] = "-.";   container['U'] = "..-";   container['_'] = "..--"; container['.'] = "---.";
    container[','] = ".-.-"; container['?'] = "----";
 
    int n;
    cin >> n;
    int Z = 1;
    while ( n > 0 ) {
        string inputString;
        cin >> inputString;
        
        vector< string > middleString;
        string middle;
        vector< int > length;
        vector< char > resultString;
 
        map< char , string >::const_iterator it_map = container.begin();
        for ( int i = 0; i < inputString.length(); ++i ) {
            it_map = container.begin();
            for ( ; it_map != container.end(); ++it_map ) {
                if ( it_map->first == inputString[ i ] ) {
                    middleString.push_back( it_map->second );
                    length.push_back( it_map->second.length() );
                }
            }
        }
        
        for ( int i = 0; i < middleString.size(); ++i ) {
            for ( int j = 0; j < middleString[ i ].length(); ++j ) {
                middle.push_back( middleString[ i ][ j ] );
            }
        }
        
        for ( int i = 0; i < middle.length(); ) {
            int getlength = length.back();
            vector< char > temp;
            string temp2;
            for ( int j = 0; j < getlength; ++j ) {
                temp.push_back( middle[ i + j ] );
            }
   
            for ( int k = 0; k < temp.size(); ++k ) {
                temp2.push_back( temp[ k ] );
            }
            
            temp.clear();
            i += getlength;
            length.pop_back();
            for ( it_map = container.begin(); it_map != container.end(); ++it_map ) {
                if ( temp2 == it_map->second ) {
                    resultString.push_back( it_map->first );
                }
            }
        }
        
        cout << Z << ": ";
        for ( int i = 0; i < resultString.size(); ++i ) {
            cout << resultString[ i ];
        }
        cout << endl;
        --n;
        ++Z;
    }
    
    return 0;
}




相关文章

相关软件