思维题: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 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...
随机推荐
- C - AtCoDeerくんと選挙速報 / AtCoDeer and Election Report
ceil有毒啊..用ceil一直错. 思路就是模拟吧,设当前的答案是ansx和ansy. 如果比例是小于ansx的,那么就要乘以一个倍数k1,使得a * k1 >= ansx的. 所以就用cei ...
- windows 2008 r2或win7安装SP1补丁,安装sqlserver 2012
说明:安装sql server 2012时,win7和win2008r2系统都需要打sp1补丁. 1.SP1补丁下载地址(建议用迅雷下载): http://download.microsoft.com ...
- MapReduce项目之气温统计
在本博文,我们要学习一个挖掘气象数据的程序.气象数据是通过分布在美国全国各地区的很多气象传感器每隔一小时进行收集,这些数据是半结构化数据且是按照记录方式存储的,因此非常适合使用 MapReduce 程 ...
- Web可用性设计的247条指导方针
首页可用性设计 首页元素要清晰的关注用户的关键任务(避免“增加功能倾向(featuritis)”) 如果网站比较大,那么首页应包含搜索输入框 首页要十分清楚的提供产品(内容)分类 在首页或首页内一次点 ...
- Unity3d网格合并
几个不同的物体,在Unity3d中可以将网格合并在一起,用于优化. 在Unity3d中构建临时场景如下:建一C#脚本名为"CombineMeshes",挂在Cube上. Combi ...
- pageContext.setAttribute的使用场合
由于页面间跳转以后,pageScope域失效,所以,关于pageContext.setAttribute和pageContext.getAttribute的使用(pagecontext作用域是page ...
- Myeclipse连接数据库删除数据库(JDBC)
package com.test.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Pr ...
- Mind must be master of the body, strong mind can separate the body from its suffering.
Mind must be master of the body, strong mind can separate the body from its suffering.意志是身体的主人,有顽强的意 ...
- web标准、可用性、可访问性
前言:大家不难发现,只要是招聘UED相关的岗位,如前端开发工程师.交互设计师.用户研究员甚至视觉设计师,一般都对web标准.可用性和可访问性的理解有要求.那么到底什么是web标准.可用性.可访问性呢? ...
- MyBatis归纳
SqlSessionTemplate详解 SqlSessionTemplate类是MyBatis-Spring的核心.这个类负责管理MyBatis的SqlSession,调用MyBatis的SQL ...