ACM详解(8)——加密

 

比赛的时候告诉你简单的加密算法,让你完成加密或者解密操作,下面通过几个简单例子介绍。
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

延伸阅读:

李绪成 CSDN Blog:http://blog.csdn.net/javaeeteacher
CSDN学生大本营:
http://student.csdn.net/space.php?uid=124362
如果喜欢我的文章,就加我为好友:
http://student.csdn.net/invite.php?u=124362&c=7be8ba2b6f3b6cc5

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值