This page is still under construction.

Parts of this page are still being built. What you see may change.

Studschiffret

Time limit1sMemory limit1024 MB

Summary
Simulate a beam that travels diagonally through an N by M grid, bouncing off walls and skipping filled cells, to reconstruct the original plaintext from the row-by-row ciphertext.
Level

Medium6 of 10

Topics
Simulation, Implementation, Matrix, Array
Solved
No attempts yet

Problem

Fretchif has invented a revolutionary cipher that nobody will be able to break! Here is how it works. You choose a string to encrypt and two integers NN and MM. Then you draw a grid with NN rows and MM columns. You then write the string you are encrypting one letter at a time diagonally down and to the right, starting from the top left corner. If we number the columns from left to right 11 to MM, and the rows from top to bottom 11 to NN, the first letter lands at position (1,1)(1,1), the second at (2,2)(2,2), the third at (3,3)(3,3), and so on. When the "letter beam" reaches one of the grid's walls, it bounces off the wall (see the explanation in the hint). If it ever lands on a cell that already has a letter in it, the letter you were about to write is written in the next free cell you reach instead. Once all the letters of the string being encrypted are used up, you read the grid row by row, and that becomes the encrypted message.

Given a message that Fretchif encrypted with the studschiffret, and given the size of the grid that was used, print the original message.

Note that some messages cannot be encrypted with some grid sizes, since it is possible to never reach a new free cell while still having letters left to place. Here, however, it is guaranteed that this did not happen when Fretchif encrypted the string.

Input

The first line contains two integers 2≤N≤202 \leq N \leq 20 and 2≤M≤202 \leq M \leq 20, the number of rows and columns in the grid. The second line contains a string of KK letters (1≤K≤301 \leq K \leq 30), the encrypted message. The message consists only of the letters A to Z, and all letters are uppercase. It is guaranteed that there exists a possible original string that produces this ciphertext.

Output

The program shall print one line with a string: the original message, as it looked before it was encrypted.

Hint

Say the string we want to encrypt is ABCDEFGHIKLMNOPQRST, and the grid has size 6×136 \times 13. Here is what the grid looks like at various points during the encryption:

After 6 letters are writtenAfter 8 letters are written, we have now bounced once off the bottom wall
After 14 letters are written, we have now also bounced off the top and right wallsAfter all 20 letters are written, note that we did not write over the H with an R, but wrote the R in the next free cell

In this example the encrypted message is therefore ATKBSJLCRIMDHNEGQOFP. Sample 2 is to decode ATKBSJLCRIMDHNEGQOFP, which decodes to ABCDEFGHIJKLMNOPQRST.

Examples4

  1. Example 1

    Input
    2 20
    PORMEIGRGAMRN
    
    Expected output
    PROGRAMMERING
    
  2. Example 2

    Input
    6 13
    ATKBSJLCRIMDHNEGQOFP
    
    Expected output
    ABCDEFGHIJKLMNOPQRST
    
  3. Example 3

    Input
    5 7
    SLUIMPEGEHTR
    
    Expected output
    SUPERHEMLIGT
    
  4. Example 4

    Input
    15 19
    DALIGREKTANGEL
    
    Expected output
    DALIGREKTANGEL