1036[Crypto Columns]

本文详细介绍了如何使用给定的关键词对文本进行转换和排序,通过创建六列网格并按照关键词字母顺序打印每一列来实现文本加密和解密。通过代码实例展示了算法实现过程,包括关键词处理、网格创建、排序和最终输出解密文本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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结果出来一堆数字吓死- -


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值