1Message Decowding
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:
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.
* Line 1: 26 lower case characters representing the decryption key
* Line 2: As many as 80 characters that are the message to be decoded
* Line 1: A single line that is the decoded message. It should have the same length as the second line of input.
Sample Input
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
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 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 "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
Sample Output
翻译:古罗马帝国有两种简单的加密算法,第一种按照顺序替换,例如把 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 即可,退出外层循环,否则继续循环。


©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页