POJ2159 Ancient Cipher
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 38430 | Accepted: 12515 |
Description
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
The lengths of both lines of the input are equal and do not exceed 100.
Output
Sample Input
JWPUDJSTVP
VICTORIOUS
Sample Output
YES
Source
首先需要明确这两种加密方式的特点:
① 凯撒加密:令明文中所有字母均在字母表上向前/后以固定值偏移并替换
② 乱序加密:给定乱序表,交换明文中每个字母的位置
解题思路先入为主肯定是通过某种手段另对明文加密或对密文解密,对结果字符串进行比较.
但是由于题目并未给出乱序表,因此此方案不可行
(若单纯只有凯撒加密,是可以通过碰撞26次偏移值做逆向还原的,
但由于还存在乱序加密,且乱序表的长度最大为100,根据排列组合来看是不可能的)
为此切入点可以通过比较明文和密文在加密前后依然保有的共有特征,猜测两者是否配对:
① 明文和密文等长
② 乱序加密只改变了明文中字母的顺序,原本的字母并没有发生变化
③ 把明文中每个字母看作一个变量,凯撒加密只改变了变量名称,该变量出现的次数没有发生变化
④ 综合①②③的条件,若分别对明文和密文每个字母进行采样,分别求得每个字母的出现频次,
然后对频次数列排序,若明文和密文是配对的,可以得到两个完全一样的频次数列
⑤ 比较频次数列会存在碰撞几率,亦即得到只是疑似解
#include <algorithm>
#include <iostream>
using namespace std; const static int MAX_LEN = ; // 密文/明文最大长度
const static int FRE_LEN = ; // 频率数组长度(记录每个字母的出现次数) /*
* 计算字符串中每个字母出现的频次
* _in_txt 入参:纯大写字母的字符数组
* _out_frequency 出参:长度为26的每个字母出现的频次数组
*/
void countFrequency(char* _in_txt, int* _out_frequency); /*
* 比对两个频次数组是否完全一致(频次数组定长为26)
* aFrequency 频次数组a
* bFrequency 频次数组b
* return true:完全一致; false:存在差异
*/
bool isSame(int* aFrequency, int* bFrequency); int main(void) {
char cipherTxt[MAX_LEN] = { '\0' }; // 密文
char plaintTxt[MAX_LEN] = { '\0' }; // 明文
int cFrequency[FRE_LEN] = { }; // 密文频次数列
int pFrequency[FRE_LEN] = { }; // 明文频次数列 cin >> cipherTxt >> plaintTxt;
countFrequency(cipherTxt, cFrequency);
countFrequency(plaintTxt, pFrequency);
cout << (isSame(cFrequency, pFrequency) ? "YES" : "NO") << endl; //system("pause");
return ;
} void countFrequency(char* _in_txt, int* _out_frequency) {
for(int i = ; *(_in_txt + i) != '\0'; i++) {
*(_out_frequency + (*(_in_txt + i) - 'A')) += ;
}
sort(_out_frequency, _out_frequency + FRE_LEN);
} bool isSame(int* aFrequency, int* bFrequency) {
bool isSame = true;
for(int i = ; i < FRE_LEN; i++) {
isSame = (*(aFrequency + i) == *(bFrequency + i));
if(isSame == false) {
break;
}
}
return isSame;
}
POJ2159 Ancient Cipher的更多相关文章
- POJ2159 ancient cipher - 思维题
2017-08-31 20:11:39 writer:pprp 一开始说好这个是个水题,就按照水题的想法来看,唉~ 最后还是懵逼了,感觉太复杂了,一开始想要排序两串字符,然后移动之类的,但是看了看 好 ...
- uva--1339 - Ancient Cipher(模拟水体系列)
1339 - Ancient Cipher Ancient Roman empire had a strong government system with various departments, ...
- UVa 1339 Ancient Cipher --- 水题
UVa 1339 题目大意:给定两个长度相同且不超过100个字符的字符串,判断能否把其中一个字符串重排后,然后对26个字母一一做一个映射,使得两个字符串相同 解题思路:字母可以重排,那么次序便不重要, ...
- Poj 2159 / OpenJudge 2159 Ancient Cipher
1.链接地址: http://poj.org/problem?id=2159 http://bailian.openjudge.cn/practice/2159 2.题目: Ancient Ciphe ...
- UVa1399.Ancient Cipher
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Ancient Cipher UVa1339
这题就真的想刘汝佳说的那样,真的需要想象力,一开始还不明白一一映射是什么意思,到底是有顺序的映射?还是没顺序的映射? 答案是没顺序的映射,只要与26个字母一一映射就行 下面给出代码 //Uva1339 ...
- poj 2159 D - Ancient Cipher 文件加密
Ancient Cipher Description Ancient Roman empire had a strong government system with various departme ...
- 2159 -- Ancient Cipher
Ancient Cipher Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 36074 Accepted: 11765 ...
- 紫书例题-Ancient Cipher
Ancient Roman empire had a strong government system with various departments, including a secret ser ...
随机推荐
- UVa 712
这个题根本不用建树,因为是完全二叉树,可以把这个想成二进制.对于根是二进制数的首位,之后依次类推.到最后的叶子节点就是从0到pow(2,n)-1. 关键在于在第一次输入的不是按照x1,x2,x3,x4 ...
- hive 非等值连接, 设置hive为nonstrict模式
1 数据准备 create table stocks(id int, date string,price string, company string); insert into table stoc ...
- Spock - Document -02 - Spock Primer
Spock Primer Peter Niederwieser, The Spock Framework TeamVersion 1.1 This chapter assumes that you h ...
- Java第五周总结
Java抽象类与接口的区别 很多常见的面试题都会出诸如抽象类和接口有什么区别,什么情况下会使用抽象类和什么情况你会使用接口这样的问题.本文我们将仔细讨论这些话题. 在讨论它们之间的不同点之前,我们先看 ...
- ROS * 了解学习urdf的内容格式及编写
<?xml version="1.0" ?> 声明文件使用xml描述 <robot name="robot_name">定义这是一个机器 ...
- linux中一些常用的目录简要说明
1.目录结构 /bin:binary的缩写,一些常用的命令如:ls.qwd.cp.cd等命令保存在这个文件内. /boot:启动linux时需要使用到的一些核心文件,以及一些镜像等,删除后系统将无法开 ...
- Linux----------samba服务的安装使用及简介
一.Samba简介 Samba是在linux和Unix系统上实现SMB协议的一个免费软件,由服务器端和客户端程序组成. Samba与nfs的不同,Samba比nfs多支持Windows SMB ( ...
- rsync命令简单用法介绍
rsync有两种常用的认证方式,一种为rsync-daemon方式,另外一种则是ssh.在一些场合,使用rsync-daemon方式会比较缺乏灵活性,ssh方式则成为首选.但是今天实际操作的时候发现当 ...
- spring事务详解(二)简单样例
系列目录 spring事务详解(一)初探事务 spring事务详解(二)简单样例 spring事务详解(三)源码详解 spring事务详解(四)测试验证 spring事务详解(五)总结提高 一.引子 ...
- gradle 打包springboot项目,找不到项目jar application.class
如题:gradle 打包springboot项目,找不到项目jar入口main方法:application.class 检查:lib/目录下没有相应项目的jar包 用gradle命令行查看日志:gra ...