Problem Description
In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 3, A would be replaced by D, B would become E, and so on. The method is named after Julius Caesar, who used it in his private correspondence.
Cipher after shifting the plain text with a shift of 3:
Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Cipher: DEFGHIJKLMNOPQRSTUVWXYZABC
Rar the Cat stumbled on the above Wikipedia article (Yes the first paragraph is copied from there) and now wants you to create a Caesar cipher machine! The machine has the following functions:
void loadCipher (string plaintext): This function would be called whenever a new plain text is loaded into the cipher machine. The current shift is reset to zero each time this function is called.
void shift (int x): Upon calling this function, the plain text should be shifted by x letters. x would be in the range -10000000 ≤ x ≤ 10000000. Note that shifts are cumulative, e.g. the function calls loadCipher("ABC"); shift(1); shift(2); would result in the string "ABC" being shifted by 3 positions.
char getLetter (int position): This function should return the letter at the specified position. The first letter would have position 1.
Your job is to code the above functions :D
Implementation Details
You are not allowed to input or output to standard input or output in any of your functions when submitting to the judge. The grader will do that automatically for you (grader.cpp).
The plain text provided would all be in uppercase and does not contain any other non-alphabetical characters.
There are template files provided under attachment, you are strongly advised to write your solution within the template files instead of creating new source files. The command to compile is g++ -O2 -o caesar grader.cpp caesar.cpp
. Alternatively, you may just run compile_cpp.sh
which contains the compilation command.
Please ask clarifications if in doubt.
Limits
Subtask 1 (20%): There will be only 1 loadCipher call and the length of the text is at most 100. There will be at most 1000 function calls in total.
Subtask 2 (35%): There will be only 1 loadCipher call and the length of the text is at most 10000. There will be at most 100000 function calls in total.
Subtask 3 (45%): There will be at most 50 loadCipher calls and the length of the text is at most 10000. There will be at most 1000000 function calls in total.
Subtask 4 (0%): Sample Testcase
Sample Testcase 1
Input:
28
L ABCDEFGHIJKLMNOPQRSTUVWXYZ
S 3
G 1
G 2
G 3
G 4
G 5
G 6
G 7
G 8
G 9
G 10
G 11
G 12
G 13
G 14
G 15
G 16
G 17
G 18
G 19
G 20
G 21
G 22
G 23
G 24
G 25
G 26
Output:
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
A
B
C