Sicily 1036. Crypto Columns

来源:互联网 发布:java socket断点下载 编辑:程序博客网 时间:2024/05/22 10:35
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
 Copy sample input to clipboard
BATBOYEYDEMBLRTHANMEKTETOEEOTHHUMDINGEIAAHEBXOIFWEHRXONNAALRSUMNREDEXCTLFTVEXPEDARTAXNAARYIEXTHEEND
Sample Output
MEETMEBYTHEOLDOAKTREENTHONCEUPONATIMEINALANDFARFARAWAYTHERELIVEDTHREEBEARSXXXXXX

分析:主要是把原文所在的列和行,和密文中的位置对应起来。对于密文中的前m个(m表示行数),对应原文中的谋一列。我们对每n个,找出它对应的列就行了。而列可以对keyword的字符排序前,记录这个字符的原始位置。比如说A原始位置是第1列。B的原始位置是第0列。代码中有部分注释

Run Memory 312KB

Run Time 0sec

#include <iostream>#include <string>#include <vector>#include <algorithm>using namespace std;bool charComp (pair<char, int> a, pair<char, int> b);int main() {        string keyword, msg;    while (cin >>keyword && keyword != "THEEND") {        cin >>msg;        vector<pair<char, int> > keyMap;        int colNum = keyword.size(),   // 列数            msgLen = msg.size();            // 密文长度        int rowNum = msgLen / keyword.size();  // 行数        for (int i = 0; i < keyword.size(); i ++)            keyMap.push_back(make_pair(keyword[i], i));   // 记录 keyword 每个字符的原始顺序        sort(keyMap.begin(), keyMap.end());                      // 对 keyword 的字符进行排序        char *ans = new char[msgLen+1];        ans[msgLen] = '\0';                // 从密文每个字符开始赋值到输出的数组中        int index = 0;        for (vector<pair<char, int> >::iterator itr = keyMap.begin(); itr != keyMap.end(); itr ++) {            int pos = itr->second;            // pos 对应的是数组中列的位置            for (int i = 0; i < rowNum; i ++)                    ans[i*colNum + pos] = msg[index++];  // 对数组一列一列赋值        }        cout <<ans <<endl;    }    return 0;}



0 0