Poj2159 comment ent Cipher
Ancient Cipher
Time Limit:1000 MS |
|
Memory Limit:65536 K |
Total Submissions:29154 |
|
Accepted:9547 |
Description
Specified ent Roman empire had a strong government system with varous parameters, 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 "Too IOUs" 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 "Too IOUs" 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 "Too IOUs" with the combination of the ciphers described above one gets the message "Too pudjstvp ".
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 cocould be the result of encrypting the message on the second line, or "NO" in the other case.
Sample Input
JWPUDJSTVPVICTORIOUS
Sample Output
YES
Question: Give you two strings and ask if the first one can get the second one through the reverse process encrypted by Caesar.
Solution: the so-called Caesar encryption will first reorganize the string through a replacement process, and then add a number to each character to become another character,
However, we cannot simulate this process because the maximum size of the string is 100. If the simulation is about 100! The running time exceeds 1 s,
We can find that the same character of a string corresponds to the same character of another string. Therefore,
If the frequency of a character in a string is n and the frequency of a character in another string is n, the two are likely to correspond,
So we can find out the frequencies of All characters in the characters, and then arrange the frequencies in order. Similarly, another string performs the same operation,
If they are arranged in the same order, they can be transformed through the inverse process encrypted by Caesar.
Reference code:
#include
#include
#include using namespace std;int main(){char x[102],y[102];int a[26],b[26];while (cin>>x>>y){int len_x=strlen(x),len_y=strlen(y),flag=0;memset(a,0,sizeof(a));memset(b,0,sizeof(b));for (int i=0;i