1579. Code Breaking

单点时限: 2.0 sec

内存限制: 256 MB

Periodic permutation is a simple encryption technique which involves choosing a period, k, and a permutation of the first k numbers. To encrypt a message, split the message into groups of k characters (padding if necessary) and apply the given permutation. Decryption involves taking groups of k characters and performing the inverse permutation. Thus for k = 4, a permutation could be 2431. This would encrypt Mary' toyMra’ and Maryan' toyMra?a?n’. Once one knows the permutation, one can apply its inverse to other encrypted messages (cyphertext) to recover the original text (plaintext).

Write a program that will read (plaintext, cyphertext1, cyphertext2) triples, and for each (plaintext, cyphertext1) pair determine whether or not a periodic permutation encryption method has been used. If it has, determine the value of k and the permutation function and apply the reverse permutation to cyphertext2 to recover the corresponding plaintext.

输入格式

Input will consist of a series of (plaintext, cyphertext1, cyphertext2) triples. Lines will be no more than 80 characters long. The first two strings (of length n) represent the first n characters of the plaintext and cyphertext. There is no implication that n is a multiple of k. The file will be terminated by a line consisting of a single #.

输出格式

Output will consist of a series of lines, one for each triple in the input. If a permutation cycle has been found, apply the inverse permutation to cyphertext2, padding it if necessary with `?’. If no periodic permutation can be found (with period less than or equal to the length of the plain and cyphertext1 strings) that transforms the plaintext into the cyphertext, then print cyphertext2 unchanged. If more than one periodic permutation could have mapped the plain text to the cyphertext1, then apply the periodic permutation that has the smallest value for k. There will never be more than one shortest permutation function that matches the data.

样例

Input
Mary had a little lamb!!
aMyrh daa l tilt ealbm!!
hTsii s aetts
Foobar
blargg
No cycle
abc
bca
abcd
#
Output
This is a test
No cycle
cab?d?

0 人解决,2 人已尝试。

0 份提交通过,共有 5 份提交。

9.9 EMB 奖励。

创建: 16 年,4 月前.

修改: 7 年,3 月前.

最后提交: 1 年,3 月前.

来源: TUD Programming Contest 1997, Darmstadt, Germany

题目标签