古老的密码(Ancient Cipher, NEERC 2004, UVa 1339) 算法竞赛入门 快排qsort

    ① Cipher:n密码,暗号,不重要的人(知识点啊!朋友们!)

    ② 映射:我觉得这里的映射,不单纯,很做作!白莲花!我的理解是,这里的映射是一个圈,A->B->C .... ->Z->A,所以无论你输入的是什么字母,它都可以任意映射到其他字母,这样一想的话,这个题目就简单多了。(然而并没有!(。・ˇдˇ・。)

    ③所以:判断条件就是:要记录下每个字母出现的次数,重新排序(升序,降序随便选,这里是用快排来做的排序),两个数组一样,这什么奇怪的古老密码就被破了。

    ④关于上面 那个映射的理解吧,其实有个小猫饼,看完我的代码我再说猫饼在哪儿

        (怎样,我就是心机(๑´ㅂ`๑) )

    ⑤算法竞赛入门书的作者说,在真的比赛里是不用qsort的,我写这个的目的就是

        闲得慌(想不到吧!Σ(゚д゚lll))

#include <stdio.h>
#include <stdlib.h> 
#include <string.h> 
//升序 
int cmp(const void *a, const void *b){
	return (*(int *)a - *(int *)b);
} 
int main()
{
	int i;
	int len1, len2;
	char str1[105], str2[105];
	int a[26], b[26];
	while(gets(str1)!= NULL)
	{
		gets(str2);
		len1 = strlen(str1);
		len2 = strlen(str2);
		if(len1 != len2)
		{
			printf("NO!\n");
		 } 
		 else
		 {
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		for(i = 0; i < len1; i++)
		{
			a[int(*(str1 + i) - 'A')]++;
			b[int(*(str2 + i) - 'A')]++;
			
		}
		//快排
		qsort(a,26,sizeof(int),cmp);
		qsort(b,26,sizeof(int),cmp);
		for(i = 0; i < 26; i++)
		{
			if(a[i] != b[i])
				break;        //当然这里可以用flag来做 
		}
		if(i == 26)
			printf("Yes!\n");
		else
			printf("NO!\n");
		 }
	}
	return 0;
} 
    猫饼就是:那古罗马帝国的人,怎么破解它们的密码?既然每个密码是可以转换成任意字母的?所以我理解的这个映射是有点猫饼的!希望知道的正确思路的朋友能告知我一下(谢谢大佬!(o°ω°o))

    

阅读更多

Ancient Cipher

12-14

DescriptionnAncient 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.nSubstitution 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".nPermutation 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".nIt 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".nArcheologists 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.nInputnInput contains 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.nThe lengths of both lines of the input are equal and do not exceed 100.nOutputnOutput "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.nSample InputnJWPUDJSTVP VICTORIOUSnSample OutputnYES

没有更多推荐了,返回首页