1. 程式人生 > >Ancient Cipher(函式與遞迴UVA1339)(排序和對映)

Ancient Cipher(函式與遞迴UVA1339)(排序和對映)

THINK:題目的意思是說有兩個字串,一個可以對映為另一個,那這樣的話只要字串中的字母在26個字元中出現過就記錄下來,並且存到兩個陣列中,把這兩個陣列排好序,看看是不是一樣的就可以了,只要是一樣的就可以把一個相應的對映成另一個

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 file contains several test cases. Each of them consists of 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 file are equal and do not exceed 100.
Output
For each test case, print one output line. 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
MAMA
ROME
HAHA
HEHE
AAA
AAA
NEERCISTHEBEST
SECRETMESSAGES
Sample Output
YES
NO
YES
YES
NO

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void arrange(int a[],int left,int right)
{
    int x=a[left], i=left,j=right;
    if(i>=j)return;
    while(i<j)
    {
        while(i<j&&a[j]>=x)j--;
        a[i]=a[j];
        while(i<j&&a[i]<=x
)i++; a[j]=a[i]; } a[i]=x; arrange(a,left,i-1); arrange(a,i+1,right); } int main() { char s1[110],s2[110]; int a[70],b[70],c[70],d[70];//第一次陣列出錯,太小了 while(~scanf("%s%s",s1,s2)) { int m=strlen(s1);//第二次是strlen用成了sizeof int n=strlen(s2); memset(a,0,sizeof(a)); memset(b,0
,sizeof(b)); memset(c,0,sizeof(c)); memset(d,0,sizeof(d)); for(int i=0;i<m;i++) { for(char j='A';j<='Z';j++) { if(s1[i]==j) { a[j-'0']++; continue; } } } /*for(int i='A'-'0';i<='Z'-'0';i++) { printf("%d ",a[i]); } printf("\n");*/ for(int i=0;i<n;i++) { for(char j='A';j<='Z';j++) { if(s2[i]==j) { b[j-'0']++; } } } /*for(int i='A'-'0';i<='Z'-'0';i++) { printf("%d ",b[i]); } printf("\n");*/ int ct=0; for(int i='A'-'0';i<='Z'-'0';i++)//第三次陣列是從什麼地方開始的並不清楚 { if(a[i]!=0) { c[ct++]=a[i]; } } /*for(int i=0;i<ct;i++)//第四次是ct寫成了ct-1;並且還是用的<號 { printf("%d ",c[i]); } printf("\n");*/ int ctt=0; for(int i='A'-'0';i<='Z'-'0';i++) { if(b[i]!=0) { d[ctt++]=b[i]; } } arrange(c,0,ct-1); arrange(d,0,ctt-1); /*for(int i=0;i<ct;i++) { printf("%d ",c[i]); } printf("\n"); for(int i=0;i<ctt;i++) { printf("%d ",d[i]); }*/ if(m==n) { int f=0; for(int i=0;i<ct;i++) { if(c[i]==d[i]) { f=1; } else { printf("NO\n"); f=0;//這裡要寫上f=0,因為如果之前的數一直是相等的, //突然有一個不相等就會來到這個else裡面,可是這時候輸出No之後 //f還是等於1的,還會再輸出一個yes break; } } if(f==1) { printf("YES\n"); } } else printf("NO\n"); } return 0; } /*int main() { for(char j='A';j<='Z';j++) { printf("%d ",j-'0'); } return 0; }*/

總結:一個在簡單不過的題就這樣犯錯犯錯犯錯,用了整整一個下午才除錯成功,吸取教訓