sicily 1036. Crypto Columns

1036. Crypto Columns

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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

BATBOY
EYDEMBLRTHANMEKTETOEEOTH
HUMDING
EIAAHEBXOIFWEHRXONNAALRSUMNREDEXCTLFTVEXPEDARTAXNAARYIEX
THEEND

Sample Output

MEETMEBYTHEOLDOAKTREENTH
ONCEUPONATIMEINALANDFARFARAWAYTHERELIVEDTHREEBEARSXXXXXX

#include<iostream>
#include<string>

using namespace std;

char rec[101][11];
//记录序列
int col[10];
int main() {
	string key, str;
	cin >> key;
	while (key != "THEEND") {
		cin >> str;
		int keyl = key.length();
		int k = 0;
		int row = str.length() % keyl == 0 ? str.length() / keyl : str.length() / keyl + 1;

		// 计算打印序列。即如题中 2 1 4 5 3 6
		for (int i = 0; i < keyl; i++) {
			char min = 'Z' + 1;
			int order = 0;
			for (int j = 0; j < keyl; j++) {
				if (key[j] < min) {
					min = key[j];
					order = j;
				}
			}
			key[order] = 'Z' + 1;
			col[k++] = order;
		}

		//根据序列填充矩阵
		k = 0;
		for (int i = 0; i < keyl; i++) {
			for (int j = 0; j < row; j++) {
				rec[j][col[i]] = str[k++];
			}
		}

		//打印原话
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < keyl; j++)
				cout << rec[i][j];
		}
		cout << endl;
		cin >> key;
	}
	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值