Shifting a Matrix
Time limit5sMemory limit512 MB
Parse a compressed shift-operation string with nested repetitions, apply the row and column rotations to an N by N matrix, and print the result.
- Level
Medium6 of 10
- Topics
- Simulation, Implementation, Array, String
- Solved
- No attempts yet
Problem
You are given an matrix whose entries start at , where is the entry in row and column . Rows and columns are numbered from 1.
Four shift operations act on the matrix.
- Left shift with rotates row one step to the left: the old becomes the new for , and the old becomes the new .
- Right shift with rotates row one step to the right: the old becomes the new for , and the old becomes the new .
- Up shift with rotates column one step up: the old becomes the new for , and the old becomes the new .
- Down shift with rotates column one step down: the old becomes the new for , and the old becomes the new .
An operation sequence is given as one string and you apply its operations from left to right. The letters 'L', 'R', 'U', and 'D' name the left, right, up, and down shifts, and the number right after a letter is the row or column to shift, so "R25" is a right shift with 25. The notation also supports repetition. A sequence wrapped in parentheses and followed by a number repeats exactly times, so "(L1R2)10" repeats the pair of a left shift with 1 and a right shift with 2, in that order, 10 times.
Every operation sequence follows this grammar:
<sequence> := <sequence><repetition> | <sequence><operation> | <repetition> | <operation>
<repetition> := '('<sequence>')'<number>
<operation> := <shift><number>
<shift> := 'L' | 'R' | 'U' | 'D'
<number> := <nonzero_digit> | <number><digit>
<digit> := '0' | <nonzero_digit>
<nonzero_digit> := '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
Given and an operation sequence, compute the matrix left after the whole sequence.
Input
The input is a single test case in this format:
N L
S
The first line contains two integers and . () is the size of the matrix and () is the length of the string on the second line. The second line contains the operation sequence , which follows the grammar above. Every row number and column number in is at least 1 and at most , and every repetition count is at least 1 and at most .
Output
Print lines. The -th line holds the integers of row of after the operations, separated by single spaces.