Crypto Columns
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
EIAAHEBXOIFWEHRXONNAALRSUMNREDEXCTLFTVEXPEDARTAXNAA
RYIEX
THEEND
Sample Output
MEETMEBYTHEOLDOAKTREENTH
ONCEUPONATIMEINALANDFARFARAWAYTHERELIVEDTHREEBEARSX
XXXXX
Source
East Central North America 2003
题目类型:模拟
算法分析:对于按照字典序遍历的每一个大写字母,遍历关键字串的每一个字符,若找到相同的字符则将关键字串的相应大小的字符以列优先加入到二维数组maps的指定列中。依次类推,最后按照行优先输出maps中的每一个字符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#include <cstdio> #include <iostream> #include <fstream> #include <cstring> using namespace std; const int maxn1 = 16; const int maxn2 = 106; char key[maxn1], cip[maxn2]; char maps[maxn1][maxn2]; int main() { //ifstream cin ("aaa.txt"); while (cin >> key) { if (!strcmp (key, "THEEND")) break; cin >> cip; int lenkey = strlen (key), lencip = strlen (cip), col = lencip / lenkey; char i; int j, p = 0; for (i = 'A'; i <= 'Z'; i++) { for (j = 0; j < lenkey; j++) { if (key[j] == i) { int k; for (k = 0; k < col; k++) maps[k][j] = cip[p*col+k]; p++;//important!!! } } } for (i = 0; i < col; i++) for (j = 0; j < lenkey; j++) printf ("%c", maps[i][j]); printf ("\n"); } return 0; } |
- « 上一篇:poj1603
- poj1651:下一篇 »