zoukankan      html  css  js  c++  java
  • 《算法竞赛入门经典》 例题 41 古老的密码(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个元素的数组分别统计每个字母对应序号出现的次数就可以了,一步一步来:
    首先是把字母都转换成对应的序号:

    m[i]-'A';
    n[i]-'A';
    

    然后用含有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;
    }
    

    结语

    哇塞,北大的在线题库都是纯英文的,由此可见程序员的英语水平是多么重要。

  • 相关阅读:
    浅谈Java中的equals和==与hashCode
    数组操作
    python-类
    javascript操控浏览器
    javascript_获取浏览器属性
    javascript_data
    python3_phantomJS_test
    python检测字符串的字母
    python百分比数比较大小
    selenium加载时间过长
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12339588.html
Copyright © 2011-2022 走看看