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.
Medium6SimulationImplementationArrayStringNo attempts yetTime limit5sMemory limit512 MBYou are given an N×N matrix A whose entries start at Ai,j=(i−1)N+j, where Ai,j is the entry in row i and column j. Rows and columns are numbered from 1.
Four shift operations act on the matrix.
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 m repeats exactly m 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 N and an operation sequence, compute the N×N matrix left after the whole sequence.
The input is a single test case in this format:
N L
S
The first line contains two integers N and L. N (1≤N≤100) is the size of the matrix and L (2≤L≤1000) is the length of the string on the second line. The second line contains the operation sequence S, which follows the grammar above. Every row number and column number in S is at least 1 and at most N, and every repetition count is at least 1 and at most 109.
Print N lines. The i-th line holds the N integers of row i of A after the operations, separated by single spaces.