码迷,mamicode.com
首页 > 其他好文 > 详细

poj2159 Ancient Cipher

时间:2015-04-03 09:33:30      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:水题   坑题   poj   频率分布   

前言

英语学的差,ac都难555.比如说这题,大牛推荐的水题,兴高采烈的来了,看完了,准备写代码时感觉到不对。果断去评论区看了下= =坑啊这。还好没直接码,题目直接理解错了有木有!同志们你们看懂了么?→_→

题目

Ancient Cipher

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 29640 Accepted: 9684

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

JWPUDJSTVP
VICTORIOUS

Sample Output

YES

分析

这里把讨论区的帖子拿出来跟大家分享:

这道题如果没看懂题意,绝对不是水题,能愁死你,而如果看懂了的话,的确稍微有点小水。
关键是对代替加密和置换加密的理解,题目中给出的例子容易误导你进入误区: 代替加密是按照一定规律来的。所以你会很容易的想到先排序,找两个字符串的差距,如果一样就YES了。。
其实,代替加密没有“规律”!A可以对应任意的26个字母。
不知道说明白了没有

所以 是否相同的标准就是 1 两个字符串初始长度是否相同 2. 频率分布是否相同(不管哪个字母的频率,只要频率从小到大排列出来,两个字符串完全相同就可以)

举个例子:
abbccc
mqqbbb
YES 频率都是 1 2 3

aabbcc
mnnjjj
NO 频率分别为 2 2 2和 1 2 3

好了,大体上就这样。

代码

然后,是代码。。。按部就班

#include <stdio.h>
#include <string.h>
#include <algorithm>

char s1[105], s2[105];

int main()
{
    gets(s1);
    gets(s2);

    int len1 = strlen(s1);
    int len2 = strlen(s2);

    if (len1 != len2)
    {
        printf("NO\n");
        return 0;
    }

    int a[26]={0};
    int b[26]={0};

    int i;
    for(i=0; i<len1; ++i)
    {
        a[s1[i]-‘A‘]++;
        b[s2[i]-‘A‘]++;
    }

    std::sort(a, a+26);
    std::sort(b, b+26);

    for (i=0; i<26; ++i)
    {
        if (a[i] != b[i])
        {
            printf("NO\n");
            return 0;
        }
    }

    printf("YES\n");


    return 0;
}

poj2159 Ancient Cipher

标签:水题   坑题   poj   频率分布   

原文地址:http://blog.csdn.net/dongmuyang/article/details/44835699

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!