Canadian Computing Competition: 1998 Stage 2, Day 1, Problem 2
Messages can be ciphered (encrypted) by systematically replacing letters of the alphabet with other letters. A simple cipher known as the Caesar cipher replaces each letter in the alphabet with the letter A
is considered to follow Z
. For example, if ABCDEFGHIJKLMNOPQRSTUVWXYZ
would be replaced by GHIJKLMNOPQRSTUVWXYZABCDEF
respectively. The message
THE FULL MOON RISING IS A BAD SIGN
would be ciphered as
ZNK LARR SUUT XOYOTM OY G HGJ YOMT
The inverse of the cipher is again a Caesar cipher with
Your job as a cryptanalyst is to decode lines of text that have been encoded with a Caesar cipher, each using a different unknown value of
ZNK LARR SUUT XOYOTM OY G HGJ YOMT
FA NQ AD ZAF FA NQ FTMF UE FTQ CGQEFUAZ
the output would be
THE FULL MOON RISING IS A BAD SIGN
TO BE OR NOT TO BE THAT IS THE QUESTION
(the first line was ciphered with
Of course, there are E
is the most common letter, with probability T
is the next more common with probability
Letter | Probability | Letter | Probability |
---|---|---|---|
A | .082 | N | .067 |
B | .015 | O | .075 |
C | .028 | P | .019 |
D | .043 | Q | .001 |
E | .127 | R | .060 |
F | .022 | S | .063 |
G | .020 | T | .091 |
H | .061 | U | .028 |
I | .061 | V | .010 |
J | .002 | W | .023 |
K | .008 | X | .001 |
L | .040 | Y | .020 |
M | .024 | Z | .001 |
Input Specification
The input to your program consists of a line containing a positive integer
The input will have at most 500 characters of input (including spaces).
Output Specification
For each line of input, give the most probable deciphering.
Note that all the data are generated such that there is only one unique most probable deciphering.
Sample Input
2
ZNK LARR SUUT XOYOTM OY G HGJ YOMT
FA NQ AD ZAF FA NQ FTMF UE FTQ CGQEFUAZ
Sample Output
THE FULL MOON RISING IS A BAD SIGN
TO BE OR NOT TO BE THAT IS THE QUESTION
Comments
There will be only inverse Cipher right? Or is it we need to consider both way?
This might be useful
C++:
Python: