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.
*****************************************************************
又是一道解密题目。首先有个Keyword。如例子中是BATBOY。这是一个六位单词,所以要把句子排成六列的二维数组,然后按BATBOY中的字母顺序按列输出。如果有相同字母则从左到右的顺序。
所以还比较简单
#include<iostream>
#include<string>
#include <algorithm>
using namespace std;
struct order
{
char word;
int num;
};
bool camp( order a, order b )
{
return a.word < b.word;
};
int main()
{
char A[10][10];
int I, J;
order O[10];
string K, W;
cin >> K;
while( K != "THEEND" )
{
cin >> W;
J = K.length();
I = W.length();
I = I / J;
// cout << I << " " << J << endl;
for(int j = 0; j < J; j++)
{
O[j].word = K[j];
O[j].num = j;
}
// for(int j = 0; j < J; j++)
// cout << O[j].word;
// cout << endl;
sort( O, O+J, camp );
// for(int j = 0; j < J; j++)
// {
// cout << O[j].word << endl;
// cout << O[j].num << endl;
// }
int count = 0;
for(int j = 0; j < J; j++)
{
for(int i = 0; i < I; i++)
{
A[i][O[j].num] = W[count];
count++;
}
}
for(int i = 0; i < I; i++)
for(int j = 0; j < J; j++)
cout << A[i][j];
cout << endl;
cin >> K;
}
return 0;
}
备注为测试用的。检测I和J的值。
之前把数组A不小心设成int结果出来一堆数字吓死- -