zoukankan      html  css  js  c++  java
  • POJ2159 Ancient Cipher

    POJ2159 Ancient Cipher
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 38430   Accepted: 12515

    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

     
    解题思路:
    首先需要明确这两种加密方式的特点:
          ① 凯撒加密:令明文中所有字母均在字母表上向前/后以固定值偏移并替换
          ② 乱序加密:给定乱序表,交换明文中每个字母的位置
     
          解题思路先入为主肯定是通过某种手段另对明文加密或对密文解密,对结果字符串进行比较.
          但是由于题目并未给出乱序表,因此此方案不可行 
          (若单纯只有凯撒加密,是可以通过碰撞26次偏移值做逆向还原的,
          但由于还存在乱序加密,且乱序表的长度最大为100,根据排列组合来看是不可能的)
     
          为此切入点可以通过比较明文和密文在加密前后依然保有的共有特征,猜测两者是否配对:
          ① 明文和密文等长
          ② 乱序加密只改变了明文中字母的顺序,原本的字母并没有发生变化
          ③ 把明文中每个字母看作一个变量,凯撒加密只改变了变量名称,该变量出现的次数没有发生变化
          ④ 综合①②③的条件,若分别对明文和密文每个字母进行采样,分别求得每个字母的出现频次,
             然后对频次数列排序,若明文和密文是配对的,可以得到两个完全一样的频次数列
          ⑤ 比较频次数列会存在碰撞几率,亦即得到只是疑似解
            
    #include <algorithm>
    #include <iostream>
    using namespace std;
     
     
    const static int MAX_LEN = 101;     // 密文/明文最大长度
    const static int FRE_LEN = 26;      // 频率数组长度(记录每个字母的出现次数)
     
     
    /* 
     * 计算字符串中每个字母出现的频次
     *   _in_txt 入参:纯大写字母的字符数组
     *   _out_frequency 出参:长度为26的每个字母出现的频次数组
     */
    void countFrequency(char* _in_txt, int* _out_frequency);
     
    /* 
     * 比对两个频次数组是否完全一致(频次数组定长为26)
     * aFrequency 频次数组a
     *  bFrequency 频次数组b
     * return true:完全一致; false:存在差异
     */
    bool isSame(int* aFrequency, int* bFrequency);
     
     
    int main(void) {
        char cipherTxt[MAX_LEN] = { '' };     // 密文
        char plaintTxt[MAX_LEN] = { '' };     // 明文
        int cFrequency[FRE_LEN] = { 0 };        // 密文频次数列
        int pFrequency[FRE_LEN] = { 0 };        // 明文频次数列
     
        cin >> cipherTxt >> plaintTxt;
        countFrequency(cipherTxt, cFrequency);
        countFrequency(plaintTxt, pFrequency);
        cout << (isSame(cFrequency, pFrequency) ? "YES" : "NO") << endl; 
     
        //system("pause");
        return 0;
    }
     
     
    void countFrequency(char* _in_txt, int* _out_frequency) {
        for(int i = 0; *(_in_txt + i) != ''; i++) {
            *(_out_frequency + (*(_in_txt + i) - 'A')) += 1;
        }
        sort(_out_frequency, _out_frequency + FRE_LEN);
    }
     
    bool isSame(int* aFrequency, int* bFrequency) {
        bool isSame = true;
        for(int i = 0; i < FRE_LEN; i++) {
            isSame = (*(aFrequency + i) == *(bFrequency + i));
            if(isSame == false) {
                break;
            }
        }
        return isSame;
    } 
  • 相关阅读:
    发现不错的cache系统Cache Manager Documentation
    List.Sort用法
    Database Initialization Strategies in Code-First:
    git rebase
    osharpV3数据库初始化
    IdentityDbContext
    AspNetUsers
    VS2015 推荐插件
    ELMAH日志组件数据库脚本
    C#如何把List of Object转换成List of T具体类型
  • 原文地址:https://www.cnblogs.com/alan-blog-TsingHua/p/10640106.html
Copyright © 2011-2022 走看看