思维题: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 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...
随机推荐
- java.lang.UnsupportedOperationException: setXIncludeAware is not supported on this JAXP implementation or earlier: class gnu.xml.dom.JAXPFactory的解决办法(图文详解)
不多说,直接上干货! 问题详情 java.lang.UnsupportedOperationException: setXIncludeAware is not supported on this J ...
- Spark Mllib里如何将如温度、湿度和风速等数值特征字段用除以***进行标准化(图文详解)
不多说,直接上干货! 具体,见 Hadoop+Spark大数据巨量分析与机器学习整合开发实战的第18章 决策树回归分类Bike Sharing数据集
- 不小心踩到的XMAPP的N种问题
1.在win10上的xampp集成环境中安装mongo扩展 按照网上搜索的下载对应文件后,在phpinfo里面还是找不到mongo的扩展信息,后面也是请教同事帮忙解决: http://www.theg ...
- js插件设置innerHTML时,在IE8下报错“未知运行时错误”
问题描述: 网站中使用了一个js插件,设置innerHTML时,在IE8下报错“未知运行时错误”: <div id=”divContainer”> <a name=”link”> ...
- (三)我的JavaScript系列:不同调用方式的this指向
人生自是有情痴,此恨不关风与月 今天所写的内容,是对之前的内容的总结和扩展.老实说,对于自己之前的一些杜撰和臆测,我并不是很满意.所以这篇博文,我希望能来点干货. 不同调用方式的this指向 在Jav ...
- codevs 4093 EZ的间谍网络
时间限制: 10 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 由于外国间谍的大量渗入,学校安全正处于高度的危机之中.YJY决定挺身而作出反抗 ...
- BZOJ 1396:识别子串 SA+树状数组+单调队列
1396: 识别子串 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 381 Solved: 243[Submit][Status][Discuss] ...
- 复杂软件的考虑点与UITableView
对象的要素.组织.整体情况: 对象的生成步骤.生成的时间节点. 考虑UITableview.
- SpringMVC-常用的注解
1. RequestParam注解 把请求中的指定名称的参数传递给控制器中的形参赋值 value:请求参数中的名称 require:请求参数中是否必须提供此参数,默认值是true,必须提供 2. Re ...
- android上部署tensorflow
https://www.jianshu.com/p/ddeb0400452f 按照这个博客就可以 https://github.com/CrystalChen1017/TSFOnAndroid 这个博 ...