POJ 2159 Ancient Cipher

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

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值