【哈希】POJ-2159 Ancient Cipher

来源:互联网 发布:linux 怎么重启mysql 编辑:程序博客网 时间:2024/06/09 13:46
Ancient Cipher
Time Limit: 1000MS Memory Limit: 65536K   

Description

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 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. 
The lengths of both lines of the input are equal and do not exceed 100.

Output

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

JWPUDJSTVPVICTORIOUS

Sample Output

YES
————————————————————恥ずかしいの分割線————————————————————
思路:看清题意!
替换并不是像叙述一样,A->B就一定要B->C。
替换可以任意进行,只要能满足一一映射就行。
对两个数列,考察各个字母出现的次数,只要每种次数可以完美匹配就满足要求。
本题哈希的思想就是,把字母哈希成出现次数。
因为我们只关心次数。次数一样,就可以替换。
代码如下:
/*ID: j.sure.1PROG:LANG: C++*//****************************************/#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <ctime>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <set>#include <string>#include <climits>#include <iostream>#define LL long longusing namespace std;const int INF = 0x3f3f3f3f;/****************************************/const int N = 111;char after[N], before[N];int A[26], B[26];int main(){#ifdef J_Sure//freopen("000.in", "r", stdin);//freopen(".out", "w", stdout);#endifwhile(~scanf("%s%s", after, before)) {int len = strlen(after), len2 = strlen(before);if(len != len2) {puts("NO");continue;}memset(A, 0, sizeof(A));memset(B, 0, sizeof(B));for(int i = 0; i < len; i++) {A[after[i] - 'A']++;B[before[i] - 'A']++;}sort(A, A+26);sort(B, B+26);if(memcmp(A, B, sizeof(A)) == 0) puts("YES");else puts("NO");}return 0;}


0 0