CCC '06 S2 - Attack of the CipherTexts

View as PDF

Submit solution

Points: 5
Time limit: 2.0s
Memory limit: 256M

Problem type
Canadian Computing Competition: 2006 Stage 1, Senior #2

Ruby is a code-breaker. She knows that the very bad people (Mr. X and Mr. Z) are sending secret messages about very bad things to each other.

However, Ruby has managed to intercept a plaintext message and the corresponding ciphertext message. By plaintext, we mean the message before it was encrypted (i.e., readable English sentences), and by ciphertext, we mean the message after it was encrypted (i.e., gibberish). To encrypt a message, each letter is changed to a new letter, so that if you read the ciphertext message, it is not obvious what the plaintext message is.

However, Ruby being the outstanding code-breaker she is, knows the algorithm that Mr. X and Mr. Z use. She knows they simply map one letter to another (possibly different) letter when they encrypt their messages. Of course, this map must be "one-to-one", meaning that each plaintext letter must correspond to exactly one ciphertext letter, as well as "onto", meaning that each ciphertext letter has exactly one corresponding plaintext letter. In this problem, there are only 27 possible characters in the plaintext and in the ciphertext - the uppercase letters and the space character.

Your job is to automate Ruby's codebreaking and help save the world.

Input Specification

The input consists of 3 strings, with each string on a separate line. The first string is the plaintext message which Ruby knows about. The second string is the ciphertext message which corresponds to the plaintext message. The third string is another ciphertext message. You may assume that all strings have length of at least 1 character and at most 80 characters. There are only 27 possible characters in any plaintext and ciphertext: the uppercase letters (A through Z) as well as the space character ( ).

Output Specification

The output is a (plaintext) string which corresponds to the second ciphertext input. It may not be possible to determine each character of the second ciphertext string, however. If this is the case, the output should have a period (.) character for those letters which cannot be determined.

Sample Input 1

THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
UIFARVJDLACSPXOAGPYAKVNQTAPWFSAUIFAMB ZAEPH
XFABSFAWFSZACBEAQFPQMFAEPJOHAWFSZACBEAUIJOHTAIBAIB

Sample Output 1

WE ARE VERY BAD PEOPLE DOING VERY BAD THINGS HA HA

Explanation for Sample Output 1

Notice that every plaintext character is in the first message, and so, the entire mapping can be computed.

Sample Input 2

THERE ARE NOT ENOUGH LETTERS
XQAZASEZASNYXSANYLWQSTAXXAZM
JSCENNYXSIACYIASXQJM

Sample Output 2

. .ANNOT .E.O.E TH.S

Explanation for Sample Output 2

Notice that the characters that cannot be determined are the (ciphertext) letters J, C, I, since these ciphertext letters do not appear in the earlier ciphertext message. It turns out that the plaintext message for the last ciphertext was I CANNOT DECODE THIS. All other letters should correspond between plaintext and ciphertext.


Comments


  • 0
    cyberboost  commented on April 7, 2024, 2:56 a.m.

    Still dont get it


  • 0
    cyberboost  commented on April 7, 2024, 1:06 a.m.

    what is the special case that u need for testcase 4?


  • 0
    daveys  commented on Feb. 10, 2024, 11:12 p.m.

    I struggled with this for a while. Got it eventually!!


    • 2
      marcus_alferez  commented on Feb. 26, 2024, 8:10 p.m.

      How did you solve the problem with test case #4? Im having issues with that one


  • 2
    Snoopoid  commented on Nov. 23, 2023, 5:45 p.m.

    Completely stumped on this, can't get past test 4. There must be something not clear in the specifications I'm not seeing.


    • 2
      aldenstripes  commented on Dec. 21, 2023, 4:13 p.m.

      Although I haven't solved the problem myself yet, I assume if there is only one character that is not mapped from both the ciphertext and plaintext letters then those two are automatically mapped to each other so you probably have to account for that.


    • 3
      bt7274  commented on Dec. 19, 2023, 6:16 a.m.

      Same here, I checked the test cases in the orignial CCC question. Test NO.4's expected output is: "THE DOG AND THE FOX". But the input didn't define what translate to 'G', so our code will just reply:"THE DO. AND THE FOX"


  • 5
    DylanJava  commented on Nov. 3, 2023, 3:38 p.m.

    Is anyone else having problems on Test NO.4?


    • 0
      bt7274  commented on Dec. 19, 2023, 6:15 a.m.

      Same here, I checked the test cases in the orignial CCC question. Test NO.4's expected output is: "THE DOG AND THE FOX". But the input didn't define what translate to 'G', so our code will just reply:"THE DO. AND THE FOX"


    • 0
      Cassarah  commented on Nov. 4, 2023, 12:32 p.m.

      I had a tough time with this as well. The samples they give show a case where all letters are decoded and a case where most letters are not decoded. My hint would be to think of other cases your program might not account for.