Time Limit: 1 secs, Memory Limit: 32 MBios
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:dom
MEETME
BYTHEO
LDOAKT
REENTHthis
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.spa
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.blog
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.排序
BATBOY EYDEMBLRTHANMEKTETOEEOTH HUMDING EIAAHEBXOIFWEHRXONNAALRSUMNREDEXCTLFTVEXPEDARTAXNAARYIEX THEEND
MEETMEBYTHEOLDOAKTREENTH ONCEUPONATIMEINALANDFARFARAWAYTHERELIVEDTHREEBEARSXXXXXX
#include <iostream> #include <algorithm> #include <map> #include <cstring> using namespace std; bool cmp (const char a, const char b) { return a < b; } int main() { string keyword; string ciphertext; while (cin >> keyword && keyword.compare("THEEND") != 0) { cin >> ciphertext; map<int, char> key_order; for (int i = 0; i < keyword.length(); i++) { key_order[i] = keyword[i]; //i表明keyword中的該字符對應的子串應該位於第幾列 } sort(keyword.begin(), keyword.end(), cmp);//對keyword進行升序排序,使得每一個字符依次與輸入的字符串的子串對應 string sub_strs[keyword.length()]; //字符列 int sub_s = 0; //每列字符串的開始座標 int sub_e = ciphertext.length() / keyword.length(); //每列字符串的結束座標 bool visited[keyword.length()]; //visited用於記錄已經訪問過得字符,避免出現如ABBCD這樣每次都訪問到第一個B的狀況 memset(visited, false, sizeof(visited)); for (int i = 0; i < keyword.length(); i++) { //遍歷排序後的keyword //對每一個字符掃描key_order來決定把keyword中的該字符放在哪一個列中 for (int j = 0; j < keyword.length(); j++) { //根據key_order以爲把對應的子串放到哪列中,即j列 if (key_order[j] == keyword[i] && visited[j] == false) { sub_strs[j] = ciphertext.substr(sub_s, sub_e); sub_s = sub_e; sub_e += ciphertext.length() / keyword.length(); visited[j] = true; break; } } } //從左到右,從上到下依次輸出 for (int i = 0; i < ciphertext.length() / keyword.length(); i++) { for (int j = 0; j < keyword.length(); j++) cout << sub_strs[j].at(i); } cout << endl; } return 0; }