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. 关于charles抓不到js文件的问题

    在清理了cookies后重新家在页面, charles抓不到js文件. 后来在https://zhidao.baidu.com/question/1802495173294727507.html 看到 ...

  2. 蓝桥杯——X星球居民问题

    [问题描述] X星球居民小区的楼房全是一样的,并且按矩阵样式排列.其楼房的编号为1,2,3... 当排满一行时,从下一行相邻的楼往反方向排号. 比如:当小区排号宽度为6时,开始情形如下: 1  2  ...

  3. MySQL_视图

    MySQL 视图 (http://www.cnblogs.com/chenpi/p/5133648.html) 1.什么是视图 通俗的讲,视图就是一条SELECT语句执行后返回的结果集.所以我们在创建 ...

  4. synchronized各种使用场景

    synchronized属于JVM锁机制 一.使用场景 在并发量比较小的情况下访问公共资源,使用synchronized是个不错的选择,但是在并发量比较高的情况下,其性能下降很严重 二.应用场景:同步 ...

  5. JavaScript中函数引用调用和函数直接调用的区别

    首先看下面的代码: var x = 1 var f1 = function( f ) { var x = 2 ; f( ' console.log( x ) ' ) } var f2 =  funct ...

  6. js···元素的属性

    Div.attributes 是所有标签属性构成的数据集合 Div.classList 是所有class名构成的数组集合 在classList的原型链上看以看到add()和remove(). clie ...

  7. install MariaDB 10.2 on Ubuntu 18

    Here are the commands to run to install MariaDB 10.2 from the MariaDB repository on your Ubuntu syst ...

  8. P2257 莫比乌斯+整除分块

    #include<bits/stdc++.h> #define ll long long using namespace std; ; int vis[maxn]; int mu[maxn ...

  9. ubuntu 上安装node.js 的简单方法

    一.安装 1.$ sudo apt-get install nodejs 2.$ sudo apt-get install npm 二.升级     1.升级npm命令如下: $ sudo npm i ...

  10. sklearn learn preprocessing

    train_test_split sklearn.model_selection.train_test_split(*arrays, test_size(float,int/None),#defaul ...