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 ...
随机推荐
- Intel的CPU漏洞:Spectre
最近觉得越来越忙,写博客都没精力了.一定是太沉迷农药和刷即刻了…… 17年年底,18年年初,Intel被爆出了Meltdown(熔断)和Spectre(幽灵)漏洞.等Spectre攻击的POC出来以后 ...
- 选择器与I/O多路复用
Selector选择器是NIO技术中的核心组件,可以将通道注册进选择器中,其主要作用是使用1个线程来对多个通道中的已就绪通道进行选择,然后就可以对选择的通道进行数据处理,属于一对多的关系,也就是使用1 ...
- 第三次Scrum冲刺————Life in CCSU
# 第三次Scrum冲刺————Life in CCSU # 一.第三次Scrum任务 小组GitHub地址链接:https://github.com/LoneylittleTeam/Team 个人G ...
- 理解 if __name__ == '__main__'
简单地理解Python中的if __name__ == '__main__' if __name__ == '__main__'的意思是: 当.py文件被直接运行时,if __name__ == '_ ...
- poj 3347
#include <cstring> #include <iostream> #include <cstdlib> #include <iomanip> ...
- vc6.0问题
1.VC6不自动提示函数的参数 是工程中的.ncb文件有问题,把这个文件删除就正常了. 2.设置代码提示快捷键 Tools-->定制-->弹出框,按照下图设置 3.报错 (1)ident ...
- git忽视上传规则文件 .gitignore
语法 语法 作用 例子 "/" 忽略根目录下的文件 /data "*" 忽略所有文件 *.txt => 1.txt,2.txt... "?&qu ...
- Visual Studio Code create the aps.net core project(Visual Studio Code 创建asp.net core项目)
Install the C# plug-in as shown below: Perfom the dotnet new --help command as shown below: Enter a ...
- Defender 防卫者
发售年份 1980 平台 街机 开发商 Williams 类型 射击 https://www.youtube.com/watch?v=gss3lxeqCok
- Pong 打乒乓
发售年份 1972 发售平台 多平台 开发商 雅达利(Atari) 类型 体育休闲 https://www.youtube.com/watch?v=fiShX2pTz9A