ECOO 2002 Boardwide Programming Contest
Problem 2: Pairwise Cipher
In war time, messages sent by radio can be heard by the enemy. It is therefore important that the message is in a secret code, so that no one but friends, who have the key, can decode the message. The key is a secret phrase that only you and your friends know: It is used to scramble the alphabet. The scrambled alphabet is next used to find the letters to substitute in the message. There are therefore two steps in coding the message: first creating the scrambled alphabet, and second, replacing the letters of the message, using the scrambled alphabet.
Suppose the key is: "Mon Oncle et ma tante"
and the message is: "Fish are birds without wings and birds are fish without fins"
First the alphabet is scrambled by means of a key as follows:
Line up the letters of the key together with the alphabet:
Now pick out all the letters, one at a time, and if a letter occurs a second time, it is deleted: We now have the scrambled alphabet:
Next the message itself is prepared:
- The letter "x" (and "X") is replaced by "ks".
- The spaces are replaced by the letter "X".
- All letters are capitalized.
- All other characters are ignored.
Encoding the message is done next.
Group all the letters in the message by twos, adding the letter X at the end of the message if necessary to make a final pair.
FI SH XA RE XB IR DS XW IT HO UT XW IN GS XA ND XB IR DS XA RE XF IS HX WI TH OU TX FI NS
Now think of each letter as having a left and a right mate, according to the scrambled alphabet. The letter L has a left mate: (C) and a right mate: (E). Even the letter M has a right mate, (O) and a left mate: (Z). In the same way, Z has a left mate (Y) and a right mate (M).
Now substitute each letter of the pairs by the following rule, using the left and right mates in:
FI: take the right mate of
(J), followed by the left mate of
SH: take the right mate of
(I), followed by the left mate of
XA: take the right mate of
(B), followed by the left mate of
and so on, to give you these new pairs:
JD IR BW TQ DW SH UB XW AH NG AS XW CH UF BW FO DW SH UB BW TQ GW UH YG JV IE VM YE JD UO
And so the final message reads:
You must write a program that will decode the messages that are received from your friends.
Your program must read 5 sets of data (a total of 10 lines). Each set of data is made up of two lines: a key phrase and an encoded message. The lines are never larger than 80 characters. You must print out both the scrambled alphabet and the decoded message. You may expect only capital letters in the encoded message (line two of each pair) and no spaces. However, the first line in each pair may contain spaces and lower case characters.
Sample Input: (only 2 of 5 sets of data)
nothing is new in this world GCIGBVWWCVLHELRVHHTTHQRVOHEIBVAZCVLHELBVWWJVEHYTGEIOVNYOGCEZ Zorro strikes again! BZABVWRMYGYEKSALKWYGALTIDTYZRGYSRRMWBZYANEYZ
NOTHIGSEWRLDABCFJKMPQUVXYZ FISH ARE BIRDS WITHOUT WINGS AND BIRDS ARE FISH WITHOUT FINS ZORSTIKEAGNBCDFHJLMPQUVWXY ONCE UPON A TIME IN MEXICO NOT SO LONG AGO
Point Value: 10
Time Limit: 2.00s
Memory Limit: 16M
Added: Mar 04, 2009
C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3