CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Practice(Peer) » Jay and Viru

Jay and Viru

Problem code: SALG05

  • Submit
  • All Submissions

All submissions for this problem are available.

Jay and Viru want to communicate with each other in the battlefield while fighting against Gabbar. They decide to make a simple cryptographic system so that the evil Gabbar does not easily decipher what they are trying to communicate. Jay's job was to develop the encryption algorithm and Viru was to develop the decryption one. Although Jay did his job, Viru is too busy romancing with Basanti. You, being Viru's close friend have to develop the decryption algorithm for him.

The encryption is done as follows:

We shall assume that inputs letters to be encrypted consists of lowercase characters (a-z) and one or more white spaces. Also whenever we refer to the term "alphabet", we always mean English Alphabet (consisting of letters a-z). Jay's encryption algorithm uses a substitution cipher (where we replace each letter of the alphabet by some other letter of the alphabet) to encrypt.

Represent each letter of the alphabet by a number in this way – a -> 0, b-> 1,..., z -> 25. If a letter is shifted by n places means that it is replaced by another letter which precedes (n < 0) or succeeds (n > 0) the given letter by n places (we go back to a in case we need to go beyond z). In case of inversion, we exchange the mapping of letters in this way – the mapping of A is now the mapping of Z and vice versa, the mapping of B is now the mapping of Y and vice versa, and so on.

Initially Jay and Viru decide upon a common word (called a key) which they would use to encrypt the text (The original text is called plaintext and the encrypted text is called ciphertext). Then the entire text (including spaces - assume there will be no leading or trailing spaces) is divided into groups of size k, where k is the length of the key. Then write the key under the groups - letter by letter. Now the first floor (k/2) letters of the plaintext under each group are shifted and inverted corresponding to the letter below it. For instance if in a group the letter a is below the letter k, k will be shifted by a, i.e. 0 places and then inverted. While the remaining letters of the group are simply shifted. Note that the shifting and inversion functions are done on letters only - white space is not encrypted. Use the above stuffs to create a decrypting algorithm.

Input

The first line of the input would consist a number T indicating the number of test cases. The next line contains a string which would be used as a key for each of the coming test cases. T test cases then follow. Each test case consists of a single line containing the cipher text.

Output

For each test case the output would consist of a single line containing the decrypted text.

Example

Input:

1
tiger
ldusf lrmcp

Output:

hello world

Explanation:

Here I shall explain how “hello world” was encrypted to form “ldusf lrmcp”. The length of the key (tiger) is 5. Divide the plain text into groups of length 5 each. The first such group is “hello”. “h” is now shifted by “t” places, i.e. 19 places and then inverted i.e. the letter corresponding to “h” now would be the letter corresponding to “s” (on applying inversion – make a table and see) which in this case is “l”. Similarly for “e”, it is “d” on shifting and inversion. The remaining three letters “l”, “l” and “o” are simply shifted which is easy to calculate. The output would be of a similar format to input – it would have spaces in the same location as the input.

The problem now asks you to build a decrypting algorithm based on the encrypting strategy described above

Input Constraints

T<=150, size of the input cipher text <= 3500 words


Author: pankajb64
Date Added: 9-02-2012
Time Limit: 0.3 sec
Source Limit: 50000 Bytes
Languages: BF, C, C99 strict, CPP 4.0.0-8, CS2, JAVA, PYTH, PYTH 3.1.2


  • Submit

Comments

  • Login or Register to post a comment.

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold

HELP

Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

 

  • Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.
  • Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.
  • Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.
  • Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.
  • Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.
  • If you are still having problems, see a sample solution here.

CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com