• POJ.2159 Ancient Cipher


    Ancient Cipher


    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


    题意分析

    看2个字符串 个数是否一一对应,是则YES,否则NO

    需要注意一下:
    Substitutes for all letters must be different. For some letters substitute letter may coincide with the original letter.

    说明替换的方法是不同的。不一定按题目的替换方法,只要被替换字母与替换字母是唯一的映射就可以了。

    解法是:
    分别对两个串的中各个字母的个数统计好后,分别放在a,b中,再排序一下。只要两个a,b两个数组完全一样就可以了。(a,b大小是26,刚好26个字母)

    再举个例子:
    addfff
    qwweee
    YES 频率都是 1 2 3

    ttyyuu
    zxxccc
    NO 频率分别为 2 2 2和 1 2 3


    /*
        Title: POJ 2159
        Date:2016-09-25
        Author: pengwill
    */
    #include<stdio.h>
    #include<stdlib.h>
    #define  max 26
    int a[max] = {0};
    int b[max] = {0};
    int comp(void const *a, void const *b){
        return *(int*)a - *(int *)b;
    }
    int main()
    {
        char ch[2];
        int exit = 0;
        while( ch[0] = getchar()){
            if(ch[0] == '
    ')
                break;
            a[ch[0] - 'A']++;
    
        }
    
        while( ch[0] = getchar()){  
            if(ch[0] == '
    ')
                break;
            b[ch[0] - 'A']++;
        }
        int i;
        qsort(a,max,sizeof(int),comp);
        qsort(b,max,sizeof(int),comp);
        for(i = 0;i<max;i++){
            if(a[i] != b[i]){
                printf("NO
    ");
                exit = 1;
                break;
            }
        } 
        if(exit == 0){
            printf("YES
    ");
        }
        return 0;
    }

    备忘录:
    1. getchar() 函数的返回值为输入字符的ASCII值,所以是个整数;
    2. 注意再用qsort()函数的时候,头文件为stdlib.h,并且有4个参数,数组收地址,长度,每个单元大小,排序方式,排序方式需要记忆一下:(以下为升序)

    int comp(void const *a, void const *b){
        return *(int*)a - *(int *)b;
    }

    若改为return (int )b - (int )a;为降序

  • 相关阅读:
    不用加减乘除做加法
    求1+2+3+...+n
    孩子们的游戏(圆圈中最后剩下的数)
    扑克牌顺子
    翻转单词顺序列
    左旋转字符串
    和为S的两个数字
    和为S的连续正数序列
    毕设进度12
    毕设进度11
  • 原文地址:https://www.cnblogs.com/pengwill/p/7367274.html
Copyright © 2020-2023  润新知