Bài 28.3. Thám Tử Tin Học: Giải Mã Caesar - [Độ khó: Khá]
Bài 28.3. Thám Tử Tin Học: Giải Mã Caesar - [Độ khó: Khá]
Bạn là một thám tử tin học và nhận được một thông điệp mật mã được mã hóa bằng thuật toán Caesar Cipher. Thuật toán này dịch chuyển mỗi chữ cái trong thông điệp đi một số vị trí cố định trong bảng chữ cái. Ví dụ, nếu dịch chuyển 3 vị trí, 'A' sẽ thành 'D', 'B' thành 'E',... và 'X' sẽ thành 'A', 'Y' thành 'B', 'Z' thành 'C' (xoay vòng). Các ký tự không phải chữ cái (như dấu cách, số, dấu câu) sẽ được giữ nguyên. Bạn cần giải mã thông điệp này.
INPUT FORMAT
Dòng đầu tiên chứa một số nguyên K
(0 <= K
<= 25) là số vị trí dịch chuyển (key).
Dòng thứ hai chứa chuỗi S
là thông điệp đã mã hóa. Chuỗi S
có độ dài không quá 200 ký tự và chứa các chữ cái tiếng Anh (hoa và thường), chữ số và dấu cách.
OUTPUT FORMAT
Một dòng duy nhất chứa chuỗi đã giải mã.
Ví dụ 1:
Input:
3
GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.
Output:
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
Giải thích:
- Key là 3. Để giải mã, chúng ta cần dịch chuyển ngược lại 3 vị trí.
'G' dịch ngược 3 vị trí (G -> F -> E -> D) sẽ là 'D'. Oh wait, this example is for encryption. Let's make it decryption.
- 'G' (thứ 7) dịch ngược 3 vị trí: (7-3) = 4, tức là 'D'.
- 'U' (thứ 21) dịch ngược 3 vị trí: (21-3) = 18, tức là 'R'.
- This example was for encryption, not decryption. Let's correct the explanation.
- 'G' (chữ cái thứ 7 trong bảng, A=1) dịch ngược 3 vị trí => chữ cái thứ (7-3)=4 là 'D'.
- Actually, the example
GUR...
->THE...
is for encryption with key 3, meaning 'T' (20) + 3 = 'W' (23). 'G' is 'D'+3. So, to decrypt, we need to shift backward byK
. - Let's assume the task is "Giải mã Caesar Cipher" meaning we need to reverse the shift.
- 'G' (ASCII 71). Key = 3.
(71 - 3 - 'A' + 26) % 26 + 'A'
if it's 'A' to 'Z'. - For 'G' (71), 'A' is 65. So G is 6th letter (index 6).
- To decrypt, index = (original_index - K + 26) % 26.
- G: index 6. (6 - 3 + 26) % 26 = 3. Index 3 is 'D'.
- U: index 20. (20 - 3 + 26) % 26 = 17. Index 17 is 'R'.
- R: index 17. (17 - 3 + 26) % 26 = 14. Index 14 is 'O'.
- This doesn't match
GUR
->THE
. The example is wrong or my understanding of standard Caesar cipher for competitive programming is. Let's assume standard Caesar cipher where A becomes D, B becomes E for K=3. So to decrypt D, it should become A. - 'G' (71). To get 'T' (84), it's (71 + x) % 26.
- It means the input is the encrypted message, and we need to decrypt it using the key
K
. So, if the original message wasTHE
, andK=3
,THE
becomesGUR
. Therefore, to getTHE
fromGUR
withK=3
, we shift backward byK
.
Let's re-verify the example.
GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.
If original wasTHE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
T (index 19) + 3 -> W (index 22) H (index 7) + 3 -> K (index 10) E (index 4) + 3 -> H (index 7) This implies the example is encryption. The problem states "giải mã", which means given K and encrypted string, output original string. Let's correct the problem description to "Mã hóa và giải mã", or change the example. A standard example for decryption with K=3: Input:3 JUBJ QEBJ
Output:
GIAI MA
Explanation:
- 'J' (thứ 9) dịch ngược 3 vị trí (9 - 3 = 6) là 'G'.
- 'U' (thứ 20) dịch ngược 3 vị trí (20 - 3 = 17) là 'R'.
- 'B' (thứ 1) dịch ngược 3 vị trí (1 - 3 + 26 = 24) là 'Y'. Oh, no, it should be (1-3) means -2, +26 for wrap around. Index 1 -> 0, A. -1 -> Z. -2 -> Y. Correct.
The example
GUR DHVPX ...
->THE QUICK ...
means thatG
is derived fromT
by shifting+3
. So, to decryptG
toT
, we need to shift by-3
. So 'G' (index 6, A=0) shifted by -3 is (6-3+26)%26 = 3, which is 'D'. This isn't 'T'. Ah,GUR
toTHE
.G
->T
impliesG
-T
= 71 - 84 = -13.U
->H
impliesU
-H
= 85 - 72 = 13. It means a shift of 13 for uppercase letters and maybe a different shift for lowercase, or it's a fixed shift but the example is confusing. Let's use the standard "rotate by K positions" logic. IfK=3
,A
becomesD
. To decrypt,D
becomesA
. So we shift backwardK
positions. Let's use a simpler example that works with this logic. Input:
Output:3 KHOOR ZRUOG
Giải thích:HELLO WORLD
K
= 3. Để giải mã, ta dịch chuyển ngược lại 3 vị trí.- Ký tự 'K' (chữ cái thứ 11 trong bảng, A=1) dịch ngược 3 vị trí: (11 - 3 = 8) là 'H'.
- Tương tự, 'H' -> 'E', 'O' -> 'L', 'O' -> 'L', 'R' -> 'O', 'Z' -> 'W', 'U' -> 'R', 'O' -> 'L', 'O' -> 'L', 'G' -> 'D'.
- Các ký tự khoảng trắng được giữ nguyên.
Comments