杰拉斯的博客

[整理]ACM详解(8)——加密

杰拉斯 杰拉斯 | 时间:2012-02-17, Fri | 6,726 views
编程算法 
比赛的时候告诉你简单的加密算法,让你完成加密或者解密操作,下面通过几个简单例子介绍。
1Message Decowding
Description
The cows are thrilled because they've just learned about encrypting messages. They think they will be able to use secret messages to plot meetings with cows on other farms.
Cows are not known for their intelligence. Their encryption method is nothing like DES or BlowFish or any of those really good secret coding methods. No, they are using a simple substitution cipher.
The cows have a decryption key and a secret message. Help them decode it. The key looks like this:
yrwhsoujgcxqbativndfezmlpk
Which means that an 'a' in the secret message really means 'y'; a 'b' in the secret message really means 'r'; a 'c' decrypts to 'w'; and so on. Blanks are not encrypted; they are simply kept in place.
Input text is in upper or lower case, both decrypt using the same decryption key, keeping the appropriate case, of course.
Input
* Line 1: 26 lower case characters representing the decryption key
* Line 2: As many as 80 characters that are the message to be decoded
Output
* Line 1: A single line that is the decoded message. It should have the same length as the second line of input.
Sample Input
eydbkmiqugjxlvtzpnwohracsf
Kifq oua zarxa suar bti yaagrj fa xtfgrj
Sample Output
Jump the fence when you seeing me coming
翻译:简单的加密算法,把字符串中的字符替换成另外的字符,只有对方知道如何替换就可以解密。题目要求根据给定的加密方法和密文,得到原始消息。
解题思路:
第一行eydbkmiqugjxlvtzpnwohracsf相当于密钥,含义是
a对应e
b对于y
c对应d
只要把密文序列中的相应字符替换为后面的字符即可。
Kifq oua zarxa suar bti yaagrj fa xtfgrj
把K替换成J
把i替换成u
把f替换成m
(注意大小写)
编程的时候:可以定义数组表示密钥。然后对密文进行遍历得到原始信息。不再给出参考代码。
2Ancient Cipher
Description
Ancient Roman empire had a strong government system with various departments, including a secret service department. Important documents were sent between provinces and the capital in encrypted form to prevent eavesdropping. The most popular ciphers in those times were so called substitution cipher and permutation cipher.
Substitution cipher changes all occurrences of each letter to some other letter. Substitutes for all letters must be different. For some letters substitute letter may coincide with the original letter. For example, applying substitution cipher that changes all letters from 'A' to 'Y' to the next ones in the alphabet, and changes 'Z' to 'A', to the message "VICTORIOUS" one gets the message "WJDUPSJPVT".
Permutation cipher applies some permutation to the letters of the message. For example, applying the permutation <2, 1, 5, 4, 3, 7, 6, 10, 9, 8> to the message "VICTORIOUS" one gets the message "IVOTCIRSUO".
It was quickly noticed that being applied separately, both substitution cipher and permutation cipher were rather weak. But when being combined, they were strong enough for those times. Thus, the most important messages were first encrypted using substitution cipher, and then the result was encrypted using permutation cipher. Encrypting the message "VICTORIOUS" with the combination of the ciphers described above one gets the message "JWPUDJSTVP".
Archeologists have recently found the message engraved on a stone plate. At the first glance it seemed completely meaningless, so it was suggested that the message was encrypted with some substitution and permutation ciphers. They have conjectured the possible text of the original message that was encrypted, and now they want to check their conjecture. They need a computer program to do it, so you have to write one.
Input
Input contains two lines. The first line contains the message engraved on the plate. Before encrypting, all spaces and punctuation marks were removed, so the encrypted message contains only capital letters of the English alphabet. The second line contains the original message that is conjectured to be encrypted in the message on the first line. It also contains only capital letters of the English alphabet.
The lengths of both lines of the input are equal and do not exceed 100.
Output
Output "YES" if the message on the first line of the input file could be the result of encrypting the message on the second line, or "NO" in the other case.
Sample Input
JWPUDJSTVP
VICTORIOUS
Sample Output
YES
翻译:古罗马帝国有两种简单的加密算法,第一种按照顺序替换,例如把a-y分别替换成b-z,把z替换成a,这样可以把VICTORIOUS替换成WJDUPSJPVT。
第二种是打乱顺序消息的顺序,例如<2, 1, 5, 4, 3, 7, 6, 10, 9, 8>的含义就是把第二个字符放在第一位,而把第一位的字符放到第二位,然后是第5个字符,第4个字符,…,可以把VICTORIOUS替换成IVOTCIRSUO。
后来发现同时使用两种算法,加密效果更好。可以把VICTORIOUS替换成JWPUDJSTVP。
题目要求:能不能把第二行中的原文转换为第一行的密文。
解题思路:首先要找出规律,第二种算法只会改变每个字符的位置,但是不会影响每个字符在字符串中出现的次数。例如A在原来的字符串中出现3次,那么通过第二种算法它出现的次数还是3次。第一种算法虽然改变了字符串的内容,但是有些东西没有变化,例如原来字符串中的a、b、c出现的次数分别n1、n2、n3,假设abc替换def,则d、e、f出现的次数应该是n1、n2、n3。所以只要保证相对位置上的字符出现的次数相同即可实现转换。
算法:
统计输入信息第一行中每个字符出现的次数。使用长度为26的数组表示,分别表示字母A到字母Z出现的次数,使用int[] a表示。
统计输入信息第二行中的每个字符出现的次数。使用长度为26的数组表示,分别表示字母A到字母Z出现的次数,使用int[] b表示。
循环26次:
第j次循环中,再循环比较a[(i+j)%26]与b[i]是否相同,如果都相同则说明能够转换,输出YES即可,退出外层循环,否则继续循环。

如果26次循环完之后,没有得到结果,输出NO。

如需转载请注明出处:杰拉斯的博客

相关文章

当前暂无评论 »