1. 程式人生 > >Bailian2820 Ancient Cryptogram【密碼】

Bailian2820 Ancient Cryptogram【密碼】

2820:Ancient Cryptogram
描述
In ancient wars, cryptogram was applied widely. There were two types of
method to encrypt a string.
The first one was replacing, which replaced every character by another. It
should use a conversion table to map the original letters to the encryped
letters. For example, if the conversion table was "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
to "BCDEFGHIJKLMNOPQRSTUVWXYZA" and the original string was "HELLO", the
encryped string should be "IFMMP".
The second one was reodering. It should use a reordering table such as
<2, 1, 5, 4, 3, 7, 6, 10, 9, 8>. Apply it to the original string
"VICTORIOUS", we got "IVOTCIRSUO" as the encrypted string.
Now, give you an original string and an encryped string. Your task is to
answer whether the original string can be converted to the encryped string using
replacing AND reodering.
輸入


Line 1: encrypted string, which contains only capital letters.

Line 2: original string, which contains only capital letter.

The length of two strings are both no more than 100.
輸出
If the original string can be converted to the encrypted string by replacing and reodering, output "YES", otherwise "NO".
樣例輸入


JWPUDJSTVP
VICTORIOUS
樣例輸出
YES

問題連結Bailian2820 Ancient Cryptogram
問題描述:(略)
問題分析:(略)
程式說明
    本題與參考連結是同一題,使用參考連結的程式提交也AC。
參考連結UVA1339 UVALive3213 POJ2159 ZOJ2658 Ancient Cipher【密碼】
題記:(略)

AC的C語言程式如下:

/* UVA1339 UVALive3213 POJ2159 ZOJ2658 Ancient Cipher */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>

#define LETTERNUM 26
#define MAXN     100

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

int main(void)
{
    char s[MAXN+1], t[MAXN+1];
    int counts[LETTERNUM], countt[LETTERNUM], len, flag, i;

    while(scanf("%s", s) != EOF) {
        scanf("%s", t);

        memset(counts, 0, sizeof(counts));
        memset(countt, 0, sizeof(countt));

        len = strlen(s);
        for(i=0; i<len; i++) {
            counts[s[i]-'A']++;
            countt[t[i]-'A']++;
        }

        qsort(counts, LETTERNUM, sizeof(counts[0]), cmp);
        qsort(countt, LETTERNUM, sizeof(countt[0]), cmp);

        flag = 1;
        for(i=0; i<LETTERNUM; i++)
            if(counts[i] != countt[i]) {
                flag = 0;
                break;
            }

        printf("%s\n", flag ? "YES" : "NO");
    }

    return 0;
}