Friday, October 15, 2010

Problem for Chatruka's Parallelogram

Raja Chatruka needs a message to be delivered to his spy. The message is supposed to be encoded so that none other than the spy can decode the message, who knows the process of encoding.


Earlier the Raja and the Spy both decided to adopt the process of “expanding parallelogram code”. The method encodes the message by placing the characters in an odd order parallelogram matrix and then reading it in a clockwise spiral from the center. Each cell of the parallelogram contains two characters; the one below is filled first as well as read first. The length of message determines the order of the matrix. The order needs to be minimum. If the number of characters are less than the number of cells in the matrix the remaining cells are filled with “/”. No message contains the actual character /.


For example: If the message is “this is a problem”



The output should be “a s this iprm/leob”


Your program must be able to encode and decode the message by this process.


INPUT The input will consist of a pair of lines. The first line will contain either of the two words “ENCODE” or “DECODE” in uppercase letters. The second line will contain the message(less than 100 characters) to be encoded or decoded depending upon the first line.


OUTPUT The output will consist of three lines. The first line will print “OUTPUT IS” in uppercase letters. The second line will print the original message, and the third line the resultant message. The character „/‟ should be removed to get the decoded message.


Note: Order of a matrix refers to the number of rows or columns.


SAMPLE INPUT:


ENCODE


this is a problem


SAMPLE OUTPUT:


OUTPUT IS


this is a problem a s this iprm/leob

1 comment: