[算法竞赛入门经典]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 ...
随机推荐
- Aspnet mvc移除WebFormViewEngine
为了提高mvc的速度,在Global.asax中移除WebFormViewEngine protected void Application_Start() { RemoveWebFormEngine ...
- java中的重写与重载
重写(Override) 重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变.即外壳不变,核心重写! 重写的好处在于子类可以根据需要,定义特定于自己的行为. 也就是说子类 ...
- python 中 try catch finally语句中含有return语句的执行情况
无论是在try还是在except中,遇到return时,只要设定了finally语句,就会中断当前的return语句,跳转到finally中执行,如果finally中遇到return语句,就直接返回, ...
- 使用FreeHttp强制登出微信公众号登陆状态(实现~原理)
概述 我们使用的部分网站设计成一旦登录即不允许用户手动退出,现实场景中是没有问题的 但如果是在测试或调试过程中就会有强制登出的需求 如果当前使用的是PC浏览器,您或许可以通过调试模式清除保持登录信息的 ...
- 【故障公告】推荐系统中转站撑爆服务器 TCP 连接引发的故障
上周五下午,我们在博客中部署了推荐系统,在博文下方显示“最新IT新闻”的地方显示自动推荐的关联博文.我们用的推荐系统是第四范式的推荐服务,我们自己只是搭建了一个推荐系统中转站(基于 ASP.NET C ...
- HashMap、Hashtable、ConcurrentHashMap的原理与区别
同步首发:http://www.yuanrengu.com/index.php/2017-01-17.html 如果你去面试,面试官不问你这个问题,你来找我^_^ 下面直接来干货,先说这三个Map的区 ...
- Git-删除文件后找回-比较文件差异
#前提:删除前,文件存在是的状态提交到了本地库#操作: git reset --hard 指针位置 删除操作已近提交到本地库:指针指向历史记录 linxianli@VM-QS- MINGW64 /c/ ...
- 编译安装MySQL5.6失败的相关问题解决方案
Q0:需要安装git 解决方案: #CentOS yum install git #ubuntu apt-get install git Q1:CMAKE_CXX_COMPILER could be ...
- EL和 JSTL? 在JSP中简化 java代码的写法!
一.servlet部分 package com.aaa.servlet; import com.aaa.dao.IStudentDAO; import com.aaa.dao.Impl.Student ...
- System.IO.Pipelines: .NET上高性能IO
System.IO.Pipelines是一个新的库,旨在简化在.NET中执行高性能IO的过程.它是一个依赖.NET Standard的库,适用于所有.NET实现. Pipelines诞生于.NET C ...