思维题: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 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...
 
随机推荐
- RL_RTX函数
			
1 延时:os_itv_set(usFrequency) //设置延时周期,配合os_itv_wait使用:os_itv_wait() 是绝对延迟是包含调用前的时间, os_dly_wait() 是相 ...
 - sql server replace函数巧妙完成字符串联结
			
示例一: 需求:将'1,2,3,4,5,6,7,8,9,10'转成:'1','2','3','4','5','6','7','8','9','10' ),) set @s='1,2,3,4,5,6,7 ...
 - 小G的城堡
			
B 小 G 的城堡文件名 输入文件 输出文件 时间限制 空间限制castle.pas/c/cpp castle.in castle.out 1s 128MB题目描述小 G 家有一座城堡.城堡里面有 n ...
 - Spring Cloud--Feign服务调用组件的使用实例
			
引入依赖: 启动类上添加@EnableFeignClients注解: 写调用接口: 直接@Autowired注入服务调用接口: 底层使用了动态代理,对接口进行了实现. 并且封装了RestTemplat ...
 - SpringMVC注解方式与文件上传
			
目录: springmvc的注解方式 文件上传(上传图片,并显示) 一.注解 在类前面加上@Controller 表示该类是一个控制器在方法handleRequest 前面加上 @RequestMap ...
 - Dubbo封装rest服务返回结果
			
由于Dubbo服务考虑到一个是给其他系统通过RPC调用,另外一个是提供HTTP协议本身系统的后台管理页面,因此Dubbo返回参数在rest返回的时候配置拦截器进行处理. 在拦截器中,对返回参数封装成如 ...
 - Nmap安全扫描程序
			
Nmap安全扫描程序 下载地址:https://nmap.org/download.html#windows 参考手册:https://nmap.org/man/zh/index.html#man-d ...
 - Beta_版本发布
			
学号 姓名 201731041215 王阳 201731062302 鲜雨珂 201731062128 邓捷 201731062305 周蓉 201731062131 龙继平 201731062304 ...
 - python+selenium之处理alert弹出对话框
			
注:本篇文章转载 http://www.cnblogs.com/mengyu/p/6952774.html 在完成某些操作时会弹出对话框来提示,主要分为"警告消息框"," ...
 - python爬虫之路——对字符串的处理
			
对字符串的处理分类:分段,连接,剔除,提取,综合 连接:+,* +(加法)的使用 a='i' b=' love' c=' you' print(a+b+c) #return i love you *( ...