分享
 
 
 

浙大在线评测 1068 P,MTHBGWB

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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: bitrain@hotmail.com

#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;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有