《算法竞赛入门经典》 例题 4-1 古老的密码(Ancient Cipher) UVa 1339
題目描述
給定兩個長度相同且不超過100的字符串,判斷是否能把其中一個字符串的各個字母重排,然后對26個字母做一個一一映射,使得兩個字符串相同。
例如,JWPUDJSTVP重排后可以得到WJDUPSJPVT,然后把每個字母映射到它前一個字母(B->A,V->B,…,Z->Y,A->Z),得到VICTORIOUS。輸出兩個字符串,輸出YES或者NO。
分析
1.既然字母可以重排,則每個字母的位置并不重要,重要的是每個字母出現的次數。
1.這樣可以統計出兩個字符串中各個字母出現的次數,得到兩個數組cnt1[26]和cnt2[26]。
3.下一步需要一點想象力:只要兩個數組排序之后的結果相同,輸入的兩個串就可以通過重排和一一映射變得相同。
這樣,問題的核心就是排序。
原題:
Ancient Cipher
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 37943
Accepted: 12351
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
Source
Northeastern Europe 2004
譯文:
古密碼
時限:1000毫秒
內存限制:65536K
提交總數:37943
接受:12351
說明
古羅馬帝國有一個強大的政府系統,各部門,包括一個特勤部門。
重要文件以加密形式在各省和首都之間發送,以防止竊聽。
當時最流行的密碼是替換密碼和置換密碼。
替換密碼將每個字母出現的所有內容都更改為其他字母。
所有字母的替代品必須不同。
有些字母的替換字母可能與原字母重合。
例如,應用替換密碼,將字母表中的所有字母從“a”更改為“y”,并將“z”更改為“a”,
將“VICTORIOUS”消息更改為“wjdupsjpvt”。
置換密碼對消息的字母應用一些置換。
例如,將排列<2,1,5,4,3,7,6,10,9,8>應用于消息“Victorious”獲取消息“Ivotchirsuo”。
人們很快注意到,在單獨使用時,置換密碼和置換密碼都比較弱。
但當它們結合在一起的時候,它們就足夠強大了。
因此,首先使用替換密碼對最重要的消息進行加密,然后使用置換密碼對結果進行加密。
用上述密碼的組合對消息“勝利”進行加密,得到消息“jwpudjstvp”。
考古學家最近發現了刻在石板上的信息。
乍一看,它似乎完全沒有意義,因此有人建議用一些替換和置換密碼對消息進行加密。
他們推測了原始消息加密后可能的文本,現在他們想檢查他們的推測。
他們需要一個計算機程序來完成,所以你必須寫一個。
輸入
輸入包含兩行。
第一行包含刻在板上的信息。
在加密之前,所有的空格和標點符號都被刪除,因此加密的郵件只包含英文字母的大寫字母。
第二行包含被推測要在第一行的消息中加密的原始消息。
它也只包含英文字母表中的大寫字母。
兩行輸入的長度相等,不超過100。
輸出
如果輸入文件第一行的消息可能是第二行加密消息的結果,則輸出“是”,否則輸出“否”。
樣例輸入
JWPUDJSTVP
VICTORIOUS
樣例輸出
YES
來源
2004年東北歐
原題鏈接:
http://poj.org/problem?id=2159
我的思路
汝佳老師都說了,問題的核心就是排序,那就先寫個排序算法,因為字符的長度不會超過一百個,那么時間應該很充裕,因為我自己的算法也不咋滴,就寫個冒泡排序吧:
void bubbleSort (int a[],int n) {for(int i=1;i<=n;i++){for(int j=i+1;j<=n;j++){if(a[i]>a[j]){int b=a[i];a[i]=a[j];a[j]=b;}}} }然后要讀入兩個字符串,這個簡單
while(scanf("%s %s",m,n)==2)根據汝佳老師的分析,關鍵是要統計出兩個字符串中各個字母出現的次數,得到兩個數組cnt1[26]和cnt2[26]。
統計各個字母的出現次數,如果將兩個字符串的每個字符都遍歷一遍然后switch的話就有點ZZ了,這么想,如果把每個字母都轉換成對應的序號,然后用一個含有26個元素的數組分別統計每個字母對應序號出現的次數就可以了,一步一步來:
首先是把字母都轉換成對應的序號:
然后用含有26個元素的數組分別統計:
cnt1[m[i]-'A']; cnt2[n[i]-'A'];最后把cnt1和cnt2排序對比就可以了。
完整代碼
#include <stdio.h> #include <string.h> void bubbleSort (int a[],int n) {for(int i=1;i<=n;i++){for(int j=i+1;j<=n;j++){if(a[i]>a[j]){int b=a[i];a[i]=a[j];a[j]=b;}}} } int main () {char m[101],n[101];int cnt1[26],cnt2[26];int len=0;bool p=true;while(scanf("%s %s",m,n)==2){len=strlen(m);memset(cnt1,0,sizeof(cnt1));memset(cnt2,0,sizeof(cnt1));for(int i=1;i<=len;i++){cnt1[m[i]-'A']++;cnt2[n[i]-'A']++;}bubbleSort(cnt1,26);bubbleSort(cnt2,26);for(int i=1;i<=26;i++){if(cnt1[i]!=cnt2[i]){p=false;break;}}if(p=false){printf("NO");}else{printf("YES");}}return 0; }結語
哇塞,北大的在線題庫都是純英文的,由此可見程序員的英語水平是多么重要。
總結
以上是生活随笔為你收集整理的《算法竞赛入门经典》 例题 4-1 古老的密码(Ancient Cipher) UVa 1339的全部內容,希望文章能夠幫你解決所遇到的問題。

- 上一篇: Dev-C++实现调试功能
- 下一篇: 《算法竞赛入门经典》 习题4-1(象棋