程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> poj2159 Ancient Cipher

poj2159 Ancient Cipher

編輯:關於C++

前言

英語學的差,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 
#include 
#include 

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
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved