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;
}
分享到:
相关推荐
北大POJ2159-Ancient Cipher 解题报告+AC代码
赠送jar包:shiro-crypto-cipher-1.4.0.jar; 赠送原API文档:shiro-crypto-cipher-1.4.0-javadoc.jar; 赠送源代码:shiro-crypto-cipher-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-crypto-cipher-1.4.0....
plexus-cipher-1.4.jar
赠送jar包:shiro-crypto-cipher-1.4.0.jar; 赠送原API文档:shiro-crypto-cipher-1.4.0-javadoc.jar; 赠送源代码:shiro-crypto-cipher-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-crypto-cipher-1.4.0....
const tls_policy = require ( 'tls-cipher-list-policy' ) ; if ( ! tls_policy . checkBuiltIn ( ) ) { console . warn ( 'WARNING: Not using the built TLS Cipher List. Reverting!' ) ; tls_policy . ...
jcabi-maven-plugin.zip,辅助Maven插件
cipher-core.js
资源来自pypi官网。 资源全名:noise_cipher-0.1.3-py3-none-any.whl
Algorithm-cipher-crypt.zip,被时间遗忘的密码坟墓。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
资源分类:Python库 所属语言:Python 资源全名:ron_cipher-1.0.3.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
pku acm 2159 Ancient Cipher代码 计数排序 解题报告请访问:http://blog.csdn.net/china8848
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
最常用的加密函数源代码,已经编译通过!aes,rsa,sha,md5
北大POJ2159的源程序程序,Ancient Cipher
如何使用将 cipher-list-creator.jsp 放入你的 Tomcat 中,并执行它。目标环境Tomcat + JDK 使用 NIO/BIO 连接器进行 SSL。 在 Tomcat 7 + Java 7 / Tomcat 8 + Java 8 上测试,但写成 Tomcat 4.1 + JDK1.4.2 兼容。...
离线安装包,亲测可用
this hill chiper source code in php
php php_使用php开发的cipher加密算法之CaesarCipher_加密算法实现
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装