美文网首页
sicily_1036 Crypto Columns

sicily_1036 Crypto Columns

作者: 我什么都不知道呀 | 来源:发表于2015-10-06 22:53 被阅读320次

    题目

    Constraints

    Time Limit: 1 secs, Memory Limit: 32 MB

    Description

    The columnar encryption scheme scrambles the letters in a message (or plaintext) using a keyword as illustrated in the following example: Suppose BATBOY is the keyword and our message is MEET ME BY THE OLD OAK TREE. Since the keyword has 6 letters, we write the message (ignoring spacing and punctuation) in a grid with 6 columns, padding with random extra letters as needed:

    MEETME
    BYTHEO
    LDOAKT
    REENTH

    Here, we've padded the message with NTH. Now the message is printed out by columns, but the columns are printed in the order determined by the letters in the keyword. Since A is the letter of the keyword that comes first in the alphabet, column 2 is printed first. The next letter, B, occurs twice. In the case of a tie like this we print the columns leftmost first, so we print column 1, then column 4. This continues, printing the remaining columns in order 5, 3 and finally 6. So, the order the columns of the grid are printed would be 2, 1, 4, 5, 3, 6, in this case. This output is called the ciphertext, which in this example would be EYDEMBLRTHANMEKTETOEEOTH. Your job will be to recover the plaintext when given the keyword and the ciphertext.

    Input

    There will be multiple input sets. Each set will be 2 input lines. The first input line will hold the keyword, which will be no longer than 10 characters and will consist of all uppercase letters. The second line will be the ciphertext, which will be no longer than 100 characters and will consist of all uppercase letters. The keyword THEEND indicates end of input, in which case there will be no ciphertext to follow.

    Output

    For each input set, output one line that contains the plaintext (with any characters that were added for padding). This line should contain no spacing and should be all uppercase letters.

    Sample Input

    BATBOY
    EYDEMBLRTHANMEKTETOEEOTH
    HUMDING
    EIAAHEBXOIFWEHRXONNAALRSUMNREDEXCTLFTVEXPEDARTAXNAARYIEX
    THEEND

    Sample Output

    MEETMEBYTHEOLDOAKTREENTH
    ONCEUPONATIMEINALANDFARFARAWAYTHERELIVEDTHREEBEARSXXXXXX

    题目大意

    加密题。给出明文和密钥,求密文。

    (以batboy为密钥,明文MEETME
    1.将明文以密钥长度为列数变成矩阵
    2.将密钥的a-z对应其位置数然后再将密钥按照a-z的循序排列,得到矩阵的加密序列。例如对应位置数为:

    batboy
    123456
    

    然后排列:

    abboty
    214536
    

    214536就是矩阵列的输出顺序。

    3.将明文加密。例如,明文MEETME对应的密文就是:

    123456    214536
    MEETME => EMTMEE
    

    思路

    二位数组存矩阵,得出矩阵列输出顺序后,按照列数输出即可。

    代码

    // 1036.cpp: http://soj.sysu.edu.cn/1036
    // Copyright (c) 2014 Junjie_Huang@SYSU(SNO:13331087). All Rights Reserved.
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    
    int main() {
      string key_word, str;
      while ((cin >> key_word) && key_word != "THEEND") {
        cin >> str;
        std::vector< std::pair<char, int> > key;
        for (int i = 0; i < key_word.size(); i++)
          key.push_back(std::pair<char, int>(key_word[i], i));
        sort(key.begin(), key.end());
    
        int columns_length = str.size() / key_word.size();
        char matrix[100 + 5][100 + 5] = { 0 };
    
        std::vector< std::pair<char, int> >::iterator it;
        int i = 0;
        for (it = key.begin(); it != key.end(); it++, i++) {
          for (int j = 0; j < columns_length; j++) {
            matrix[it->second][j] = str[i*columns_length + j];
          }
        }
    
        for (int i = 0; i < columns_length; i++)
        for (int j = 0; j < key_word.size(); j++)
          cout << matrix[j][i];
        cout << endl;
      }
    }
    
    

    相关文章

      网友评论

          本文标题:sicily_1036 Crypto Columns

          本文链接:https://www.haomeiwen.com/subject/gjxncttx.html