`

Ancient Cipher--POJ2159

 
阅读更多

Ancient Cipher

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 13258

Accepted: 4719

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

Source

Northeastern Europe 2004

先读懂题意:

题意比较简单,但要从中看出其本质来。基本思想就是对一串字母明文先进行凯撒编码,但由于这样的密码很脆弱,因此在凯撒编码的基础上再进行置乱,最终得出字母密文串。

题目输入是一行密文和一行明文,输出要求判断是否能从密文得出明文,或者说从明文按上述算法进行编码能否得出密文,总之呢,如果模拟题目的算法要求,就是对输入的明文串进行两重循环,外层循环是遍历所有可能的凯撒编码,内层循环是对每一种凯撒编码得出的字符串再遍历每种可能的置乱编码(也就是进行全排列),然后将最终得出的字符串和明文字符串进行比较,如果相等就输出“YES”,结束循环;不等就继续遍历。

但如果按上面的做法,不仅编码麻烦,时间复杂度也不低,因此我们可以换个角度想一下,既然要遍历所有可能的凯撒编码,那么就是说每个明文字符都可能被字母表中除自己外的其他字母替换,而置乱只改变每个字符在字符串中的顺序。因此,字符串中的字符到底是字母表中的哪一个是无所谓的了,我们只需要记录明文串和密文串中不同的字母出现的次数,并将其按出现的次数进行排序,再比较两个序列是否相同即可,相同说明可有明文串经变换得出密文串,反之亦然。

解题思路:计数排序啦!

代码如下:

#include <iostream>

#include <string>

#include <cstdlib>

#include <algorithm>

int cipher[26]; //存放密文中字母出现次数

int plain[26]; //存放明文中字母出现次数

std::string input; //存放输入的字符串

int i;

int main()

{

memset(cipher, 0, sizeof(cipher));

memset(plain, 0, sizeof(plain));

std::cin>>input;

for(i=0; i<input.length(); i++)

{

cipher[ input[i]-'A' ]++; //对密文中字母出现次数计数

}

std::cin>>input;

for(i=0; i<input.length(); i++)

{

plain[ input[i]-'A' ]++; //对明文中字母出现次数计数

}

std::sort(cipher, cipher+26); //计数次数进行排序以用于比较

std::sort(plain, plain+26);

for(i=0; i<26; i++)

{

if(cipher[i] != plain[i]) //有一个不同说明不匹配

{

std::cout<<"NO"<<std::endl;

return 0;

}

}

std::cout<<"YES"<<std::endl; //所有的都相同,匹配

system("pause");

return 0;

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics