UVA-1339 Ancient Cipher

来源:互联网 发布:恢复中国国籍 知乎 编辑:程序博客网 时间:2024/06/05 23:54

Ancient Roman empire had a strong government system with various departments, including a secretservice department. Important documents were sent between provinces and the capital in encryptedform to prevent eavesdropping. The most popular ciphers in those times were so calledsubstitutioncipherand permutation cipher.

Substitution cipher changes all occurrences of each letter to some other letter. Substitutes for allletters must be different. For some letters substitute letter may coincide with the original letter. Forexample, applying substitution cipher that changes all letters from ‘A’ to ‘Y’ to the next ones in thealphabet, 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, ap-plying the permutation2,1,5,4,3,7,6,10,9,8to the message “VICTORIOUS” one gets the message“IVOTCIRSUO”.

It was quickly noticed that being applied separately, both substitution cipher and permutationcipher 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 wasencrypted using permutation cipher. Encrypting the message “VICTORIOUS” with the combination ofthe ciphers described above one gets the message “JWPUDJSTVP”.

Archeologists have recently found the message engraved on a stone plate. At the first glance itseemed completely meaningless, so it was suggested that the message was encrypted with some substi-tution and permutation ciphers. They have conjectured the possible text of the original message thatwas 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 themessage engraved on the plate. Before encrypting, all spaces and punctuation marks were removed, sothe encrypted message contains only capital letters of the English alphabet. The second line containsthe original message that is conjectured to be encrypted in the message on the first line. It also containsonly 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 filecould be the result of encrypting the message on the second line, or ‘NO’ in the other case.

Sample Input

JWPUDJSTVPVICTORIOUSMAMAROME
HAHAHEHEAAAAAANEERCISTHEBESTSECRETMESSAGES

Sample Output

YESNOYESYESNO

心路历程:第一次尝试使用qsort函数进行排序,使用这个方法要比冒泡排序省时许多。这个题目的思想就是字母出现的次数,以及出现的字母类型要相同。也就是这两个字符串里的不同字符出现的比例要相同(两个字符串长度一定一样)。其实这样一想就十分简便了。

PS:在comp函数里一定要加const,开始因为学校oj的编译器版本老,所以没有检查出这个方面的错误,直到后来我才发现。

#include<stdio.h>#include<string.h>#include<stdlib.h>int comp(const void *a, const void *b){    return *(int*)a - *(int*)b;}int main(){    int cnt1[110], cnt2[110];    char str1[110], str2[110];    while(gets(str1)!=NULL)    {        int flag = 1;        memset(cnt1, 0, sizeof(cnt1));        memset(cnt2, 0, sizeof(cnt2));        gets(str2);        int len = strlen(str1);        for(int i = 0; i < len; i++)        {            cnt1[str1[i] - 'A']++;            cnt2[str2[i] - 'A']++;        }        qsort(cnt1, 26, sizeof(cnt1[0]), comp);        qsort(cnt2, 26, sizeof(cnt2[0]), comp);        for(int i = 0; i < 26; i++)        {            if(cnt1[i] != cnt2[i])            {                printf("NO\n");                flag = 0;                break;            }        }        if(flag == 1)            printf("YES\n");    }    return 0;}


0 0