[算法竞赛入门经典]Ancient Cipher, NEERC 2004,UVa1339
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 file contains several test cases. Each of them consists of 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 file are equal and do not exceed 100.
Output
For each test case, print one output line. 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
MAMA
ROME
HAHA
HEHE
AAA
AAA
NEERCISTHEBEST
SECRETMESSAGES
Sample Output
YES
NO
YES
YES
NO
Code
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cctype>
#define MAX 1005
using namespace std;
char s1[MAX],s2[MAX];
int cnt1[26],cnt2[26];
int cmp(const void* a,const void* b){
return *(int*)a - *(int*)b;
}
int main()
{
while(~scanf("%s %s",s1,s2)){
int len1 = strlen(s1),len2 = strlen(s2);
for(int i = 0;i < len1;i ++) ++cnt1[s1[i]-'A'];
for(int j = 0;j < len2;j ++) ++cnt2[s2[j]-'A'];
qsort(cnt1,26,sizeof(int),cmp);
qsort(cnt2,26,sizeof(int),cmp);
int ok = 1;
for(int i = 0;i < 26;i ++)
if(cnt1[i]!=cnt2[i]) { ok = 0; break; }
if(ok) printf("YES\n");
else printf("NO\n");
memset(cnt1,0,sizeof(cnt1)); memset(cnt2,0,sizeof(cnt2));
}
return 0;
}
[算法竞赛入门经典]Ancient Cipher, NEERC 2004,UVa1339的更多相关文章
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
- [刷题]算法竞赛入门经典 3-12/UVa11809
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa11809:Floating-Point Numbers 代码: //UVa11 ...
- [刷题]算法竞赛入门经典 3-10/UVa1587 3-11/UVa1588
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-10/UVa1587:Box 代码: //UVa1587 - Box #include&l ...
- [刷题]算法竞赛入门经典 3-7/UVa1368 3-8/UVa202 3-9/UVa10340
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 都是<算法竞赛入门经典(第二版)>的题目,标题上没写(第二版) 题目:算法竞赛入门经典 3-7/UVa13 ...
- [刷题]算法竞赛入门经典 3-4/UVa455 3-5/UVa227 3-6/UVa232
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa455:Periodic Strings 代码: //UVa455 #inclu ...
- [刷题]算法竞赛入门经典 3-1/UVa1585 3-2/UVa1586 3-3/UVa1225
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO(我也是在网上找到的pdf,但不记得是从哪里搜刮到的了,就重新上传了一遍) PS:第一次写博客分享我的代码,不知道我对c ...
- 算法竞赛入门经典训练指南——UVA 11300 preading the Wealth
A Communist regime is trying to redistribute wealth in a village. They have have decided to sit ever ...
- 算法竞赛入门经典+挑战编程+USACO
下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...
- 算法竞赛入门经典 LA 4329(树状数组)
题意: 一排有着不同能力值的人比赛,规定裁判的序号只能在两人之间,而且技能值也只能在两人之间 问题: <算法竞赛入门经典-训练指南>的分析: 上代码: #include<iostre ...
随机推荐
- JavaScript 最终将在编程语言中占统治地位?
JavaScript 最终将在编程语言中占统治地位? JavaScript 现在是大多数开发者都会使用的编程语言.网络效应会推动它成为有史以来第一个真正占统治地位的编程语言吗? 大约十年前,编程的方式 ...
- JavaScript match()方法和正则表达式match()
先介绍参数为普通字符串的使用方式,此时match方法的返回值是存放首次匹配内容的数组.如果没有找到匹配结果,返回null.语法结构: 1 str.match(searchvalue)参数解析:(1). ...
- 三。Hibernate 注解形式
Hibernate支持JPA注解的jar包 JPA全称: Java Persistence API JPA和Hibernate之间的关系,可以简单的理解为JPA是标准接口,Hibernate是实现. ...
- 华硕飞行堡垒fx50 安装ubuntu18.04
决定把我的渣机脱坑 一.制作启动盘 官方下载ubuntu18.04LTS iso文件 [ubuntu官方链接](https://www.ubuntu.com/download/desktop Ultr ...
- Python学习案例之Web版语音合成播报
前言 语音合成技术能将用户输入的文字,转换成流畅自然的语音输出,并且可以支持语速.音调.音量设置,打破传统文字式人机交互的方式,让人机沟通更自然. 应用场景 将游戏场景中的公告.任务或派单信息通过语音 ...
- 08 Django REST Framework 解决前后端分离项目中的跨域问题
01-安装模块 pip install django-cors-headers 02-添加到INSTALL_APPS中 INSTALLED_APPS = ( ... 'corsheaders', .. ...
- scala的多种集合的使用(5)之数组Array(ArrayBuffer)的操作
1.创建和更新数组的不同方式 1)定义一个数组的初始大小和类型,随后填充值. scala> val array = new Array[String](3) array: Array[Strin ...
- simpledet 的配置
simpledet 的配置 1. 通过 docker 配置 simpledet 1.1 系统要求 ubuntu16.04 python >=3.5 1.2 下载 docker 镜像 匹配的版本为 ...
- linux 上 mysql 的使用
1.登录mysql 第一次登录 没有密码 可以直接输入 mysql 有密码可以使用 mysql -u root -p 回车会提示需要输入密码 -u 用户名 -p 密码 这个mysql文件在/us ...
- plus webview关闭事件监听
plus.webview.currentWebview().addEventListener("close",function(){ },false);