Delta Encoding and Decoding
Time limit1sMemory limit128 MB
Implement a stateful command interpreter that encrypts, decrypts, or redefines a letter-substitution cipher based on positional value differences, with exact output formatting.
- Level
Medium6 of 10
- Topics
- String, Implementation, Simulation
- Solved
- No attempts yet
Problem
A delta cipher is a very simple encryption scheme that is a big improvement over a substitution cipher. Each of the 26 letters A–Z is assigned a numeric value from 0 to 25. The assignment is one-to-one (a permutation of the letters), but not necessarily in alphabetical order. The default cipher assigns A = 0, B = 1, …, Z = 25, and is used until a valid new cipher is supplied.
Encryption is based on the differences between the values of successive letters.
- Every character that is not a letter is copied to the output unchanged.
- When a letter is not immediately preceded by another letter (including the first character of the text), it is treated as though it were preceded by the letter
A. - Case is preserved: an uppercase input letter stays uppercase and a lowercase input letter stays lowercase. However,
Aandadenote the same letter and therefore share the same value. - When a letter is preceded by another letter, take the value of the current letter minus the value of the preceding letter. The letter whose value equals this difference taken modulo 26 replaces the current letter in the output (keeping the current letter's case).
Decryption reverses the process, reconstructing the original text one letter at a time.
Input
The input is a sequence of commands, one per line, terminated by end of file. No line contains more than 10000 characters. There are three commands, and the case of the letters in a command word does not matter. Each command word is followed by at least one space.
ENCRYPT— the rest of the line is plain text to encrypt with the current cipher.DECRYPT— the rest of the line is encrypted text to convert back to plain text with the current cipher.CIPHER— the rest of the line defines a new cipher. It must contain exactly twenty-six letters with no repetitions; spaces and punctuation are ignored. Their order fixes the values: the first letter has value 0, the second has value 1, and so on.
Any line whose first word is none of these three commands is not understood.
Output
Produce one line of output for each input line. The spacing shown below is part of the required output and must be reproduced exactly; in particular, every output line ends with a single trailing space.
- For
ENCRYPTandDECRYPT: the textRESULT:then two spaces, then the transformed text, then one trailing space. - For a valid
CIPHER: the textGood cipher.then two spaces, thenUsing, a space, the twenty-six letters in uppercase, then a period and one trailing space. The new cipher is used for all following commands. - For an invalid
CIPHER(wrong number of letters or a repeated letter): the textBad cipher.then two spaces, thenUsing default.and one trailing space; the default cipher is used from then on. - For an unrecognized command: the text
Command not understood.followed by one trailing space.