Shake, Rattle and Roll

Time limit1sMemory limit128 MB

Summary
Fill a square grid with an uppercase message plus padding letters, then apply the key's sequence of shake, rattle, and roll operations and print the final flat string.
Level

Medium4 of 10

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

Problem

Sam wants to send secret messages to Sally, and vice versa, so they devise a simple but effective encryption scheme they can perform by hand. As their messages grow longer and longer, they decide the scheme needs to be automated. Your job is to implement their "shake, rattle and roll" encryption scheme. (Decryption is not required.)

A text message is placed into a square 2D array in row-major order, one character per cell. Every character is stored as a capital letter. If the message does not completely fill the array, the remaining cells are filled with capital letters of the alphabet starting at A and continuing through Z, repeating as needed. The matrix is always square, and its size ranges from 3×3 to 100×100. For example, the message "Meet me at the pizza parlor" placed into a 6×6 array looks like the figure below (note that every character is stored as a capital letter).

To encrypt the message, three kinds of operations are performed. Each operation is applied once for every time it appears in the key, in the order given.

Shake — Columns are numbered starting at 1. Each odd-numbered column is shifted up by one character, and the topmost character wraps around to the bottom of that column. Each even-numbered column is shifted down by one character, and the bottommost character wraps around to the top. For example:

Rattle — Rows are numbered starting at 1 from the top. Each odd-numbered row is shifted right by one character, and the rightmost character wraps around to the leftmost position of that row. Each even-numbered row is shifted left by one character, and the leftmost character wraps around to the rightmost position. For example:

Roll — Consider the concentric rings ("loops") of the matrix. Each loop is numbered by the row index of its topmost row (the outermost loop's top row is row 1). Each odd-numbered loop is rotated one character clockwise (to the right), and each even-numbered loop is rotated one character counter-clockwise (to the left). For example:

Input

The input contains one or more encryption problems. Each problem occupies two lines. The first line is the encryption key; the second line is the text to encrypt.

An encryption key always begins with a two-digit matrix size, followed by a sequence of the characters S, R, and L in any order. Each S performs one shake, each R performs one rattle, and each L performs one roll, applied in the order they appear. A size of 00 is interpreted as 100.

The key is at most 80 characters long. Each message is at most 10,000 characters and is guaranteed to fit inside the specified matrix.

Output

For each problem, print the encrypted text on its own line. The length of the encrypted text equals the matrix size squared (for example, a 3×3 matrix produces a string of length 9).

Examples5

  1. Example 1

    Input
    04RSRR
    I love ice cream
    06SRL
    Meet me at the Pizza Parlor
    
    Expected output
    IREAELCIMVE   OC
    EIEEAGTTIMT E P ZHRZB PAORDAFLEA CMH
    
  2. Example 2

    Input
    03S
    ABCDEFGHI
    
    Expected output
    DHFGBIAEC
    
  3. Example 3

    Input
    03R
    ABCDEFGHI
    
    Expected output
    CABEFDIGH
    
  4. Example 4

    Input
    03L
    ABCDEFGHI
    
    Expected output
    DABGECHIF
    
  5. Example 5

    Input
    03
    ABCDEFGHI
    
    Expected output
    ABCDEFGHI