2004 Canadian Computing Competition, Stage 1

Problem J4: Simple Encryption

One of the simplest ways of coding a message is to do a letter shift.

For example, if you shift the letters in the original message by 5 then A in your original message becomes F in the coded message. (B → G, C → H, ..., T → Y, U → Z, V → A, ..., Z → E). To decode the message, you simply need to shift back by the same number.

A slightly trickier encryption uses a keyword to determine the amount of the shift. Suppose you were using a keyword "ACT". To encode the message, you take the original message, remove everthing but the alphabetic characters, and form the message into a block that has the same width as the keyword. Here is a sample message to encrypt:

BANANA & PEEL

The blocked version of the message is shown below with the keyword ACT as a header.

A C T
     
B A N
A N A
P E E
L    

Now, the message is encoded using a letter shift. However, this time it is not a uniform shift, it will depend upon the keyword letter at the top of the column. If the letter at the top of the column is an "A", the the letters in that column are not shifted. If the letter is a "B", then the letter in that column shift by 1, and so on. In the example, the letters in the third column will shift by 19 since the "T" is the 20th letter of the alphabet.

The encoded message is:

A C T
     
B C G
A P T
P G X
L    

You will write a program that will accept a keyword and a string to be encoded. The keyword will never have more than 6 characters. The message will always be given in all upper case characters. The total message length will never exceed 60 characters.

Input

The first line of input consists of the keyword. The second line of the input is the message to be encoded. The keyword length will never exceed 6 characters. The total message length also will never exceed 60 characters.

Output

Output the encoded message on a single line.

Sample Input 1

ACT
BANANA & PEEL

Sample Output 1

BCGAPTPGXL

Sample Input 2

TRICKY
I LOVE PROGRAMMING!

Sample Output 2

BCWXONKFOTKKFZVI

All Submissions
Best Solutions


Point Value: 5
Time Limit: 2.00s
Memory Limit: 16M
Added: Oct 03, 2008

Problem Types: [Show]

Languages Allowed:
C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3

Comments (Search)

This problem should be worth more!

Why do you say that? The algorithm involved is extremely simple.

Char math!

An overused question. I can only get 4/5? Is there something wrong with my submission?

To encode the message, you take the original message, remove everthing [sic] but the alphabetic characters

I did! The first third thing I did was to delete everything other than the alphabetic characters. And what does
everthing [sic] but
mean?

But you didn't do it correctly. As your loop is written, it always skips the character right after any character that it deletes (trace through if you don't see why). Also, see this.

Ok...now I get it.

And, now I don't! Okay, I fix up all the problems, and guess what! TIME LIMIT EXCEEDED. Really disappointed to see today's tech dwarfed by a max of 66 loops XD.

Your program must be going into an infinite loop, meaning that it never stops. Try looking at it more closely to see why that would happen, or try finding a test case for which it occurs (as it only happens under certain circumstances).

Hint: Write a sample test case in which the output is all the same letter. See if you program works.