POJ2159 Ancient Cipher
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 38430   Accepted: 12515

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

 
解题思路:
首先需要明确这两种加密方式的特点:
      ① 凯撒加密:令明文中所有字母均在字母表上向前/后以固定值偏移并替换
      ② 乱序加密:给定乱序表,交换明文中每个字母的位置
 
      解题思路先入为主肯定是通过某种手段另对明文加密或对密文解密,对结果字符串进行比较.
      但是由于题目并未给出乱序表,因此此方案不可行
      (若单纯只有凯撒加密,是可以通过碰撞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的更多相关文章

  1. POJ2159 ancient cipher - 思维题

    2017-08-31 20:11:39 writer:pprp 一开始说好这个是个水题,就按照水题的想法来看,唉~ 最后还是懵逼了,感觉太复杂了,一开始想要排序两串字符,然后移动之类的,但是看了看 好 ...

  2. uva--1339 - Ancient Cipher(模拟水体系列)

    1339 - Ancient Cipher Ancient Roman empire had a strong government system with various departments, ...

  3. UVa 1339 Ancient Cipher --- 水题

    UVa 1339 题目大意:给定两个长度相同且不超过100个字符的字符串,判断能否把其中一个字符串重排后,然后对26个字母一一做一个映射,使得两个字符串相同 解题思路:字母可以重排,那么次序便不重要, ...

  4. Poj 2159 / OpenJudge 2159 Ancient Cipher

    1.链接地址: http://poj.org/problem?id=2159 http://bailian.openjudge.cn/practice/2159 2.题目: Ancient Ciphe ...

  5. UVa1399.Ancient Cipher

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  6. Ancient Cipher UVa1339

    这题就真的想刘汝佳说的那样,真的需要想象力,一开始还不明白一一映射是什么意思,到底是有顺序的映射?还是没顺序的映射? 答案是没顺序的映射,只要与26个字母一一映射就行 下面给出代码 //Uva1339 ...

  7. poj 2159 D - Ancient Cipher 文件加密

    Ancient Cipher Description Ancient Roman empire had a strong government system with various departme ...

  8. 2159 -- Ancient Cipher

    Ancient Cipher Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36074   Accepted: 11765 ...

  9. 紫书例题-Ancient Cipher

    Ancient Roman empire had a strong government system with various departments, including a secret ser ...

随机推荐

  1. ffmpeg推送直播流的技术进展

    首先安装好NGINX并打开服务 然后安装好ffmpeg 然后参考:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=2879051 ...

  2. linux 常用命令积累

    工作中常用的linux记录一下,方便查询使用 1.ln 创建连接 ,就是window上的快捷方式 创建软连接  ln -s 源文件 目标文件名   经常访问的文件夹(项目文件夹),在~创建一个软连很方 ...

  3. python矩阵的切片(或截取)

    矩阵一般有行也有列,所以矩阵的截取也需要包含行和列两个参数. 假设a是一个矩阵,a的截取就可写成:a[起始行:终止行,起始列:终止列],中括号中有一个逗号,逗号前的是为了分割行的,逗号后的是为了分割列 ...

  4. Mac 10.13.6 安装 cocoapods

    卸载pod sudo rm -fr ~/Library/Caches/CocoaPods/ sudo rm -fr ~/.cocoapods/repos/master/ sudo rm -fr Pod ...

  5. 内联元素padding与高度可控的分隔线实例页面

    html: <a href="">登录</a><a href="">注册</a> css: a + a:befo ...

  6. JVM垃圾收集器-Parallel Scavenge收集器

    今天我给大家讲讲JVM垃圾收集器-Parallel Scavenge收集器 Parallel Scavenge收集器 Parallel Scavenge收集器也是一个新生代收集器,它也是使用复制算法的 ...

  7. [USACO07OPEN]便宜的回文Cheapest Palindrome

    字串S长M,由N个小写字母构成.欲通过增删字母将其变为回文串,增删特定字母花费不同,求最小花费.        题目描述见上            显然 这是一道区间DP 从两头DP,枚举长度啥的很套 ...

  8. linx6.7 update openssh to 7.7p1

    升级之前需要注意几点: 1 必须要有自己的镜像,必须自己做好本地yum源(可以连接外网,能够有网络yum源也可以) 2 配置好基本的升级环境.在升级openssh时需要依赖openssl和zlib.一 ...

  9. FlexRay通信机制

    通信周期是 FlexRay 的基本通信单元[6~7,19].每个通信周期包括四个时间层,如图 2-9 所示.通信周期由静态段(Static Segment).动态段(Dynamic Segment). ...

  10. android BluetoothAdapter蓝牙BLE扫描总结

    1.android 4.3.1(Build.VERSION_CODES.JELLY_BEAN_MR2)增加的startLeScan(callback)方法,官方在5.0之后不建议使用,实测此方法,4. ...