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;
}*/

总结:一个在简单不过的题就这样犯错犯错犯错,用了整整一个下午才调试成功,吸取教训

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值