Ancient Cipher,NEETC 2004,UVa1339

来源:互联网 发布:2017淘宝店铺开店流程 编辑:程序博客网 时间:2024/06/04 18:15

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



题目大意:给定两个长度相同且不超过100的字符串,判断是否能把其中一个字符串的字母重排,然后对26个字母做一个一一映射,使得两个字符串相同。例如JWPUDJSTVP

重排后是WJDUPSJPVT,然后把每个字母映射到它前一个字母(B->A,C->B.....Z->Y,A->Z),如果两个字符串经过变换可以相等输出YES,否则输出NO.


分析:不用考虑字母的顺序(因为顺序可以随意变换),只需考虑各个字母的个数,如果个数相同的字母一一对应,就能够变换为相等字符串。我们只需统计各个字母分别在两个字符串a[ ],b[ ]中出现的次数,并用a1[ ], b1[ ]记录下来。然后把a1[ ], b1[ ]按数组元素从小到大排序,最后比较a1[i],与b1[i]是否相等。

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;int main(){    char a[105],b[105];    while(cin>>a>>b)    {        int len=strlen(a);        int a1[26],b1[26];        memset(a1,0,sizeof(a1));//需要清除数组        memset(b1,0,sizeof(b1));        for(int i=0;i<len;i++) //遍历字符串统计各个字母出现的次数        {            a1[a[i]-'A']++;            b1[b[i]-'A']++;        }        sort(a1,a1+26);         //将各个字母的出现次数排序        sort(b1,b1+26);        for(int i=0;i<26;i++)   //比较次数相同的字母在是否个数对应        {            if(a1[i]!=b1[i])            {                 cout<<"NO"<<endl;                 break;            }            if(i==25)                cout<<"YES"<<endl;        }    }    return 0;}




原创粉丝点击