DecodeMoveToFront
TCCC '03 Semifinals 4 · 2003-04-04 · by vorthys
Problem Statement
Consider a simple encryption algorithm based on the move-to-front heuristic.
Both the unencrypted (plaintext) and encrypted (ciphertext) messages are
Encryption processes the plaintext from left to right, outputting one character of the ciphertext for each character of the plaintext. At each position of the plaintext, the encryption algorithm performs the following steps:
- Find the (zero-based) position of the plaintext character in the current permutation.
- If the position is 0-25, output 'A'-'Z', respectively. If the position is 26, output a space.
- Move the plaintext character to the front of the permutation (ie, delete it from its current position in the permutation and re-insert it at the front).
- State = "ZYXWVUTSRQPON MLKJIHGFEDCBA". 'T' is in position 6, output 'G'.
- State = "TZYXWVUSRQPON MLKJIHGFEDCBA". 'P' is in position 10, output 'K'.
- State = "PTZYXWVUSRQON MLKJIHGFEDCBA". 'C' is in position 24, output 'Y'.
- State = "CPTZYXWVUSRQON MLKJIHGFEDBA". 'D' is in position 24, output 'Y'.
- State = "DCPTZYXWVUSRQON MLKJIHGFEBA". 'R' is in position 11, output 'L'.
You will be given both the plaintexts and the ciphertexts of several messages, where the i-th ciphertext
is believed to be the encrypted form of the i-th plaintext, and all the messages are believed
to have been encrypted using the same key. Your task is to recover and return that key (as a
Notice that, when encrypting multiple messages, the state is not carried over from one message to the next, but rather is re-initialized for each message.
Constraints
- plaintexts and ciphertexts contain the same number of elements (between 1 and 20, inclusive).
- Element i of plaintexts has the same length as element i of ciphertexts (between 1 and 50 characters, inclusive).
- Elements of plaintexts and ciphertexts contain uppercase letters ('A'-'Z') and spaces only.
{"TPCDR"}
{"GKYYL"}
Returns: "------T-R-P------------DC--"
The example above, but the characters that were not used could have been anywhere in the initial permutation.
{"A","B"}
{"X","X"}
Returns: "ERROR"
A and B cannot both be in position 23 of the key.
{"A","A"}
{"X","Y"}
Returns: "ERROR"
{"ABA"}
{"XYX"}
Returns: "ERROR"
{"ABCDEFGHIJKL NOPQRSTUVWXYZ"}
{"ABCDEFGHIJKLMNOPQRSTUVWXYZ"}
Returns: "ABCDEFGHIJKL NOPQRSTUVWXYZM"
When 26 characters have been used, the other is known by process of elimination.
{"HELLO"}
{"HOWDY"}
Returns: "ERROR"
The second 'L' should be encoded as an 'A'.
{"HI"}
{"AA"}
Returns: "ERROR"
The 'I' cannot be encoded as 'A' because 'H' is guaranteed to be in that position.
Submissions are judged against all 51 archived test cases, of which 7 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class DecodeMoveToFront with a public method string findKey(vector<string> plaintexts, vector<string> ciphertexts) · 51 test cases · 2 s / 256 MB per case