今天遇到一道特别恶心的水题,可能是我水平太low,居然用了好久的时间!不过主要是用在看懂题意上了,英文题目好长,好恶心啊!!题目如下:
Description
| 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
Output
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
题目翻译:
给你一个摩斯码对照表,26个字母以及下划线‘_’、逗号','、顿号'.'(英文的一个点)、问号'?'分别对应上面和下面表格的摩斯码,输入只会包含大写字母与后面四个符号,输入给你一个测试数据数n,然后是n组数据,每组一行由英文字母和下划线、逗号
、问号、顿号组成的字符串,要你翻译出加密后的内容,其中,翻译过程如下:
第一步:加密,也就是将字母翻译成摩斯密码,然后每个字母记录其对应的摩斯码的长度;
第二步:反转数字串:
第三步:解码,将摩斯码与数字串结合起来对应摩斯码的表格翻译出转换后的字符串即可。
例如:输入:
1
AKADTOF_IBOETATUK_IJN
第一步:加密-->.--.-.--..----..-...--..-...---.-.--..--.-..--...----.232313442431121334242
第二步:反转数字串-->242433121134244313232
第三步:解码翻译成转换后的字符串--> ACM_GREATER_NY_REGION
Ok啦!!
下面是我写了半天的代码,以及别人写的代码,感觉其实差不太多,一并附上:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#include <cstdio>// P,MTHBGWB
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#define INF 0x7fffffff
#define FIN 0x80000000
using namespace std;
const char letter[32] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
'O','P','Q','R','S','T','U','V','W','X','Y','Z','_',',','.','?'
};
const char *code[]= {".-","-...","-.-.","-..",".","..-.","--.",
"....","..",".---","-.-",".-..","--","-.",
"---",".--.","--.-",".-.","...","-","..-",
"...-",".--","-..-","-.--","--..",
"..--",".-.-","---.","----"
};
const int num[] = {2,4,4,3,1,4,3,
4,2,4,3,4,2,2,
3,4,4,3,3,1,3,
4,3,4,4,4,
4,4,4,4
};
void transforms(char *str)
{
int len = strlen(str);
int number[105];
int t = 0;
string temp ;
for(int i=0 ; i<len ; i++)//加密
{
for(int j=0 ; j<30 ; j++)
{
if(str[i]==letter[j])
{
temp.append(code[j]);//append方法用于往字符串后添加字符串
number[t++] = num[j];
}
}
}
int k = 0;
for(int i=0 ; i<len ; i++) //解密
{
for(int j=0 ; j<30 ; j++)
{
if((temp.compare(k,number[len-1-i],code[j]))==0)
{
cout<<letter[j];
break;
}
}
k += number[len-1-i] ;
}
cout<<endl;
}
int main()
{
int n;
cin>>n;
char str[105];
for (int i=1; i<=n; i++)//循环输出
{
cin>>str;
cout<<i<<": ";
transforms(str);
}
return 0;
}
别人的:
地址:http://blog.youkuaiyun.com/ziren235/article/details/1363408
FAQ | About Virtual Judge | Forum | Discuss | Open Source Project
All Copyright Reserved ©2010-2014 HUST ACM/ICPC TEAM
Anything about the OJ, please ask in the forum, or contact author: Isun
Server Time: 2017-05-01 09:20:05
- string str3="Hello";
本文介绍了如何解决一道涉及摩斯码加密、数字串反转和解码的编程题。首先,根据摩斯码表将输入的字母和符号加密为摩斯码,记录每个字符对应的摩斯码长度;然后,反转这个数字串;最后,结合摩斯码进行解码,还原出原始字符串。示例中给出了加密和解密的具体步骤,以及两段用于字符串操作的示例代码。

被折叠的 条评论
为什么被折叠?



