2159:Ancient Cipher

  • 查看
  • 提交
  • 统计
  • 提问

时间限制:

1000ms

内存限制:

65536kB

描述

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 (1)"VICTORIOUS" one gets the message (2)"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 (3)"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 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 "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.

样例输入

JWPUDJSTVP

VICTORIOUS

样例输出

YES

  • 查看
  • 提交
  • 统计
  • 提问

Tips:

明文与密文的“字母频率的数组”应该是一样的,即明文中某字母出现8次,密文中也必须有某个字母出现8次。

所以字母种类,字母频率同时相等时,即被破解。

#include"iostream"
#include"string"
using namespace std;
int main()
{
 string initial,encrypted;
 cin>>initial;
 cin>>encrypted;
 int a[26],b[26];//标记initial和encrypted的26个字母的出现频率
 for(int i=0;i<26;i++)
     a[i]=0;
 for(int i=0;i<26;i++)
     b[i]=0;
 for(int i=0;i<initial.size();i++)
 {
  a[initial[i]-'A']++;
  b[encrypted[i]-'A']++;
 }
 int i,j;
 for(i=0;i<26;i++)
 {
  for(j=0;j<26;j++)
   if(a[i]==b[j])
   {
    b[j]=-1;//标记已经取出的encrypted的频率
    break;
   }
  if(j==26)//说明initial的某个字母的频率所有encrypted的字母频率均无法匹配
   break;
 }
 if(j==26)//匹配失败
  cout<<"NO"<<endl;
 else if(i==26)//匹配成功
  cout<<"YES"<<endl;
}

转载于:https://www.cnblogs.com/lzhitian/archive/2011/08/16/2140074.html

POJ-2159(Water)相关推荐

  1. POJ 2159 解题报告

    一.substitution cipher (置换密码): Substitution cipher changes all occurrences of each letter to some oth ...

  2. POJ 1205 Water Treatment Plants(递推)

    题意   建设一条河岸的污水处理系统  河岸有n个城市   每一个城市都能够自己处理污水 V   也能够把污水传到相邻的城市处理 >或<   除了你传给我我也传给你这样的情况   其他都是 ...

  3. POJ 2159 分组密码与流密码

    因为既要替换又要排列,而方法却有很多种,无法明确确定.但是知道明文中一些相同字符出现的次数跟密文出现的次数相同.所以可以根据这一点来进行计算. #include<iostream> #in ...

  4. POJ1003/1004/1005/1207/3299/2159/1083/3094/2388解题(刷一波水题)

    POJ 1003 题目链接 http://poj.org/problem?id=1003 大意:长度=1/2+1/3+-+1/n,给定长度值,求n #include<iostream> u ...

  5. 《题目与解读》红书 训练笔记目录《ACM国际大学生程序设计竞赛题目与解读》

    虽然2012年出版的老书了,但是是由三次世界冠军的上海交大ACM队出版的书籍,选择的题目是ACM经典中的经典,书中有非常详细的题解,可以学到很多东西,值得一刷. 目录 第一部分 第一章 数学 1.1 ...

  6. The Water Bowls POJ - 3185(开关问题+暴力)

    题意: 给20个水碗.朝上为'0'或朝下为'1',每次操作使三个碗翻转,问使所有20个水碗都朝上,至少翻多少次? 题目: The cows have a line of 20 water bowls ...

  7. poj 1205 :Water Treatment Plants (DP+高精度)

    题意:有n个城市,它们由一个污水处理系统连接着,每个城市可以选择 1.将左边城市过来的污水和右边城市过来的污水连同本身的污水排到河里  >V< 2.将左边来的污水连同自己的污水排到右边   ...

  8. POJ前面的题目算法思路【转】

    1000 A+B Problem 送分题 49% 2005-5-7 1001 Exponentiation 高精度 85% 2005-5-7 1002 487-3279 n/a 90% 2005-5- ...

  9. poj题目详细分类及算法推荐题目

    DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  1024   Calendar Game       简单题  ...

  10. ACM POJ 题目分类(完整整理版本)

    DP: 1011   NTA                 简单题  1013   Great Equipment     简单题  1024   Calendar Game       简单题   ...

最新文章

  1. 亮剑:PHP,我的未来不是梦(3)
  2. volatile与synchronized的区别
  3. was java sdk_关于WAS9单独安装SDK的问题?
  4. SQL Server数据库漏洞评估了解一下
  5. SAP License:SAP CO ML 物料帐配置
  6. java中ares框架_ARES辅助开发工具-用户手册.doc
  7. OpenGL基础25:多光源(附简单GLSL配置)
  8. C函数改写成汇编语言函数,帮忙将一个C函数写成汇编语言
  9. 【穷举】用c#实现一个数组(1,1,2,2,3,3,4,4)排列,每两个相同数字中间都间隔了这个数字个数...
  10. 爱pia戏推出PC客户端,为您自动置顶窗口,方便查找
  11. 实地踩坑,新鲜出炉,阿里云GPU服务器Centos7.7深度学习环境搭建实战
  12. 使用js实现鼠标放置时显示下拉列表
  13. oracle begin 后声明,Oracle BEGIN END 详细用法
  14. UTC世界协调时间和BJT北京时间的转换
  15. 如何表格合并快速简单?
  16. 玩《刀塔传奇》,玩的就是一种策略
  17. Modbus串行传输方式
  18. 【LeetCode刷题】栈与队列专题
  19. 钳形表校准典型校准方法
  20. Linux核心安装(转)

热门文章

  1. python获取文本光标_使用python readline时如何获取(并设置)当前bash光标位置?
  2. 小程序聊天室开发,发送文字,表情,图片,音频,视频,即时通讯,快速部署,可定制开发
  3. uni-app 音频控制
  4. 视频录制,压缩实现源码
  5. WPF实用指南二:移除窗体的图标
  6. 看看Vector源码Java 9
  7. 建立CentOS 6.9 的Yum本地源
  8. subst将文件夹目录虚拟成虚拟磁盘
  9. unity替换mesh测试
  10. RegularExpressions(4) RegularExpressions 成员(一)