思维题:UVa1334-Ancient Cipher
Ancient Cipher
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
解题心得:
- 题意就是给你两个字符串,可以把一种字母替换成一种另字母(例如:a->z,y->b),也可以在字符串中将字符互相交换,问是否可以通过这两个规则将一个字符串变成另一个字符串,可以YES,否则NO。
- 这个题真的不要想的太复杂,首先字符串长度要相同,然后可以直接统计出每个字符在字符串中的个数,然后排序,只要两个数列是相同的就可以直接YES了。
- 因为可以字符互相交换,所有可以直接排序不用在意字符串本身的顺序,然后统计每个字符串的个数,只要字符种类数目相同并且相同字符个数相同就可以,因为可以替换不用在意这个数目的字符到底是什么。由于不用在意顺序,也不用在意字符本身到底是什么,所以就可以将两个字符串抽象成两个有序的数列,然后直接比较两个数列。
4.
#include<bits/stdc++.h>
using namespace std;
const int maxn = 110;
char s1[maxn],s2[maxn];
int cha1[maxn],cha2[maxn];//将两个字符串抽象成两个数列
set<char> se1;//记录第一个字符串中字符的种类
set<char> se2;//记录第二个字符串中字符的种类
int _find ()//统计出每个串的每个字符的个数
{
int len1 = strlen(s1);
int len2 = strlen(s2);
for(int i=0;i<len1;i++)
se1.insert(s1[i]);
for(int i=0;i<len2;i++)
se2.insert(s2[i]);
set<char>::iterator iter;
int t1 = 0,t2 = 0;
for(iter = se1.begin();iter!=se1.end();iter++)
{
int num = 0;
for(int i=0;i<len1;i++)
if(s1[i] == *iter)
num++;
cha1[t1++] = num;
}
for(iter = se2.begin();iter!=se2.end();iter++)
{
int num = 0;
for(int i=0;i<len2;i++)
if(s2[i] == *iter)
num++;
cha2[t2++] = num;
}
sort(cha1,cha1+t1);
sort(cha2,cha2+t2);
return max(t1,t2);
}
int main()
{
while(~scanf("%s%s",s1,s2))
{
if(strlen(s1) != strlen(s2))//当长度不相同可以直接排除
{
printf("NO\n");
continue;
}
memset(cha1,0,sizeof(cha1));
memset(cha2,0,sizeof(cha2));
se1.clear();//别忘了要清空
se2.clear();
int len = _find();
bool flag = false;
for(int i=0;i<len;i++)
{
if(cha1[i] != cha2[i])//只要每个字符对应的数目相等就可以
{
flag = true;
break;
}
}
if(flag)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}
思维题:UVa1334-Ancient Cipher的更多相关文章
- POJ2159 ancient cipher - 思维题
2017-08-31 20:11:39 writer:pprp 一开始说好这个是个水题,就按照水题的想法来看,唉~ 最后还是懵逼了,感觉太复杂了,一开始想要排序两串字符,然后移动之类的,但是看了看 好 ...
- UVa 1339 Ancient Cipher --- 水题
UVa 1339 题目大意:给定两个长度相同且不超过100个字符的字符串,判断能否把其中一个字符串重排后,然后对26个字母一一做一个映射,使得两个字符串相同 解题思路:字母可以重排,那么次序便不重要, ...
- UVa1399.Ancient Cipher
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Ancient Cipher UVa1339
这题就真的想刘汝佳说的那样,真的需要想象力,一开始还不明白一一映射是什么意思,到底是有顺序的映射?还是没顺序的映射? 答案是没顺序的映射,只要与26个字母一一映射就行 下面给出代码 //Uva1339 ...
- uva--1339 - Ancient Cipher(模拟水体系列)
1339 - Ancient Cipher Ancient Roman empire had a strong government system with various departments, ...
- zoj 3778 Talented Chef(思维题)
题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...
- cf A. Inna and Pink Pony(思维题)
题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...
- Poj 2159 / OpenJudge 2159 Ancient Cipher
1.链接地址: http://poj.org/problem?id=2159 http://bailian.openjudge.cn/practice/2159 2.题目: Ancient Ciphe ...
- ZOJ 3829 贪心 思维题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...
随机推荐
- (转)linux dumpe2fs命令
linux dumpe2fs命令 命令名称 dumpe2fs - 显示ext2/ext3/ext4文件系统信息. dumpe2fs命令语法 dumpe2fs [ -bfhixV ] [ -o supe ...
- Jenkins+Gitlab+Ansible自动化部署(一)
首先准备实验环境 虚拟机 主机名 IP地址 服务 系统版本 内核版本 Vmware Workstation 14 gitlab.example.com 192.168.244.130 gitlab ...
- 牛客网Java刷题知识点之关键字static、static成员变量、static成员方法、static代码块和static内部类
不多说,直接上干货! 牛客网Java刷题知识点之关键字static static代表着什么 在Java中并不存在全局变量的概念,但是我们可以通过static来实现一个“伪全局”的概念,在Java中st ...
- 【Java】 jar解压与压缩
jar解压与压缩 命令格式:jar {c t x u f }[ v m e 0 M i ][-C 目录]文件名 # 解压,到当前目录 jar -xvf source.jar # 打包,不进行压缩 ja ...
- bjut193E 吃饭时的怪癖
题目: http://bjutacm.openjudge.cn/lianxi/193E/ 思路: n的所有质因数之和等于phi(n) * n / 2, phi(n)为欧拉函数. 实现: #includ ...
- querySelector/querySelectorAll
querySelector获取页面I属性D为test的元素: document.getElementById("test"); //or document.querySelecto ...
- java-web项目:用servlet监听器实现显示在线人数
1.创建一个监听器 package com.listener; import javax.servlet.ServletContext; import javax.servlet.http.HttpS ...
- deb软件安装
deb是debian linux的安装格式,跟red hat的rpm非常相似,最基本的安装命令是:dpkg -i file.deb dpkg 是Debian Package的简写,是为Debian 专 ...
- sshd_config配置注释
# $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm Exp $ # This is the sshd server system-wide c ...
- WebClient UI和Tomcat的启动器
WebClient UI 我们在WebClient UI的开发工具里点了Test按钮, 会在浏览器以测试模式打开选中的view.这背后发生了什么事?注意浏览器地址栏的bspwd_cmp_test,这是 ...