CCC '12 J4 - Big Bang Secrets

View as PDF

Submit solution

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

Problem type
Canadian Computing Competition: 2012 Stage 1, Junior #4

Sheldon and Leonard are physicists who are fixated on the BIG BANG theory. In order to exchange secret insights, they have devised a code that encodes UPPERCASE words by shifting their letters forward.

Shifting a letter by S positions means to go forward S letters in the alphabet. For example, shifting B by S = 3 positions gives E. However, sometimes this makes us go past Z, the last letter of the alphabet. Whenever this happens we wrap around, treating A as the letter that follows Z. For example, shifting Z by S = 2 positions gives B.

Sheldon and Leonard's code depends on a parameter K and also varies depending on the position of each letter in the word. For the letter at position P, they use the shift value of S = 3P + K.

For example, here is how ZOOM is encoded when K = 3. The first letter Z has a shift value of S = 3 \times 1 + 3 = 6; it wraps around and becomes the letter F. The second letter, O, has S = 3 \times 2 + 3 = 9 and becomes X. The last two letters become A and B. So Sheldon sends Leonard the secret message: FXAB

Write a program for Leonard that will decode messages sent by Sheldon.

Input Specification

The input will be two lines. The first line will contain the positive integer K (K < 10), which is used to compute the shift value. The second line of input will be the word, which will be a sequence of uppercase characters of length at most 20.

Output Specification

The output will be the decoded word of uppercase letters.

Sample Input 1


Output for Sample Input 1


Sample Input 2


Output for Sample Input 2



  • 2
    lidonghan  commented on Dec. 3, 2022, 11:57 a.m. edited

    Taking S \bmod 26 also works.

  • 1
    Rhumph  commented on Sept. 13, 2022, 2:10 p.m.

    Looking for a little help…in the second sample…first letter, s=5*1+5=10. If you move 10 spots to the left of j, you get z. The shift s should be 8. What am I missing? Thanks.

    • 1
      shibendu  commented on Sept. 13, 2022, 2:18 p.m. edited

      Shift is 3P+K, where position P of 1st letter 'B' is 1. So shift is 3 \times 1+5=8. Therefore J becomes B after left shift.

      • 0
        Rhumph  commented on Sept. 13, 2022, 3:34 p.m.

        Thank you

  • 1
    mahes0640  commented on Feb. 27, 2021, 2:54 p.m.

    How do the sample outputs work? When I put 3 and FXAB, and do this formula 3×1+3, how does it go to Z? My program got LGMQ, because when you add the 6 to F (at pos 6) you get to 12 which is L?

    • 8
      Josh  commented on Feb. 27, 2021, 3:38 p.m.

      The question asks you to decode a message, not encode a message.

  • -1
    100nik0v  commented on Nov. 18, 2020, 10:57 p.m. edited

    Is it just my inexperience, or do you have to use a separate if else loop for every separate letter?

  • 2
    Orion222  commented on May 15, 2020, 11:44 p.m.

    bruh make the word decode 10 times bigger

  • 1
    Harryg33107  commented on Dec. 5, 2019, 2:15 a.m.

    My submission produced an AC result. But after I tested it with the following input


    It gave the following output:


    How come?

    • 6
      hxxr  commented on Dec. 7, 2019, 10:13 p.m. edited

      It is possible for the ASCII character code after decoding to be less than 39. Simply adding 26 to the result will not always make it into a valid letter—sometimes you will need to add a multiple of 26.