POJ 2159 Ancient Cipher
Ancient Cipher
|
Time Limit: 1000MS |
Memory Limit: 65536K |
|
Total Submissions: 35432 |
Accepted: 11550 |
Description
Ancient Romanempire had a strong government system with various departments, including asecret service department. Important documents were sent between provinces andthe capital in encrypted form to prevent
eavesdropping. The most popularciphers in those times were so called substitution cipher and permutationcipher.
Substitution cipher changes all occurrences of each letter to some otherletter. Substitutes for all letters must be different. For some letterssubstitute letter may coincide with the original letter. For example, applyingsubstitution cipher that changes all
letters from 'A' to 'Y' to the next onesin 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. Forexample, applying the permutation <2, 1, 5, 4, 3, 7, 6, 10, 9, 8> to themessage "VICTORIOUS" one gets the message"IVOTCIRSUO".
It was quickly noticed that being applied separately, both substitution cipherand permutation cipher were rather weak. But when being combined, they werestrong enough for those times. Thus, the most important messages were firstencrypted using substitution
cipher, and then the result was encrypted usingpermutation cipher. Encrypting the message "VICTORIOUS" with thecombination of the ciphers described above one gets the message"JWPUDJSTVP".
Archeologists have recently found the message engraved on a stone plate. At thefirst glance it seemed completely meaningless, so it was suggested that themessage was encrypted with some substitution and permutation ciphers. They haveconjectured the possible
text of the original message that was encrypted, andnow they want to check their conjecture. They need a computer program to do it,so you have to write one.
Input
Input contains twolines. The first line contains the message engraved on the plate. Beforeencrypting, all spaces and punctuation marks were removed, so the encryptedmessage contains only capital letters of
the English alphabet. The second linecontains the original message that is conjectured to be encrypted in themessage on the first line. It also contains only capital letters of the Englishalphabet.
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 theresult of encrypting the message on the second line, or "NO" in theother case.
Sample Input
JWPUDJSTVP
VICTORIOUS
Sample Output
YES
本题需要注意到substitution cipher不仅仅只有平移的方式…所以只需要统计一下两个字符串中字母出现的频率,如果相等即为YES;
代码:
//poj 2159c Ancient Cipher
#define LOCAL
#include <cstdio>
#include <string>
#include <algorithm>
int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
int letter1[26],letter2[26];
memset(letter1,0,sizeof letter1);
memset(letter2,0,sizeof letter2);
char message[200],encrypted[200];
scanf("%s",encrypted)!
for(int i=0;message[i];i++) letter1[message[i]-'A']++;
scanf("%s",encrypted);
for(int i=0;encrypted[i];i++) letter2[encrypted[i]-'A']++;
sort(letter1,letter1+26);
sort(letter2,letter2+26);
for(int i=0;i<26;i++)
{if(letter1[i]!=letter2[i]) printf("NO\n");return 0}
printf("YES\n");
return 0
}
本文介绍POJ 2159 AncientCipher题目背景与要求,该题目涉及古代罗马加密方法,包括置换密码和替代密码的组合使用。文章提供了问题描述、输入输出样例及解题思路,通过统计字符频率来判断两字符串是否符合加密规则。
244

被折叠的 条评论
为什么被折叠?



