Formatting Software
Problem Description
Vivek is developing software that takes a set of numerical instructions, processes them, and formats them as required.
Vivek is working on a big project, and he asked you to implement a software that takes a set of numerical information in the form of r x c matrices, processes them, and formats them according to the given instructions. The instructions consist of multiple lines, with each line containing one or more integer values. Each integer specifies which matrix should be printed.
For example, if the instructions are like below,
4
1 2 3
which says that you should first print matrix number 4 from the information provided. Then, on the subsequent lines, you need to print matrices 1, 2, and 3 sequentially.
Given N, representing the number of matrices, and two additional integers r and c representing the number of rows and columns in each matrix, along with the N matrices provided contiguously over r lines, and in the decribed format, print the formatted information.
(Refer Examples section for better understanding of the input)
Constraints
1 <= N <= 10
1 <= r, c <= 10
1 <= numbers in the matrix <= 100
1 <= number of matrices in the instructions <= 20
Matrices are numbered from 1 to N; thus, the instructions will include only numbers within this range. Note that numbers may be repeated.
Input
First line consists of an integer N, denoting the number of matrices
Second line consists of two space separated integers denoting the number of rows and columns in each matrix.
The next r lines contain the N matrices, each of size r × c, listed sequentially. Each matrix will have integers separated by spaces, and consecutive matrices will be separated by a space as well.
Remaining lines represent the instructions of how the information should be formatted.
Output
Print the formatted information following the given instructions.
Time Limit (secs)
1
Examples
Example 1
Input
7
1 3
5 4 12 11 19 3 0 15 7 1 2 3 4 5 6 10 9 8 3 60 71
5
7 1 3
2 4 5
3 6
4
Output
4 5 6
3 60 71 5 4 12 0 15 7
11 19 3 1 2 3 4 5 6
0 15 7 10 9 8
1 2 3
Explanation
If you split the given data into 1x3 submatrices and print them in the specified format, you will obtain the output. Let's go over the input processing.
Line 4 i.e. input 5 asks to print matrix number 5. Output 4 5 6 corresponds to this input.
Line 5 i.e. input 7 1 3 asks to print matrices 7, 1 and 3. Output 3 60 71 5 4 12 0 15 7 corresponds to this input.
Similarly processing line number 6, 7 and 8 of the input, we obtain lines 3, 4 and 5 of the output.
Example 2
Input
4
2 3
1 2 3 4 5 6 7 8 9 10 11 12
12 11 10 9 8 7 6 5 4 3 2 1
1 2 3
4 1
2 3
3
1 4
Output
1 2 3 4 5 6 7 8 9
12 11 10 9 8 7 6 5 4
10 11 12 1 2 3
3 2 1 12 11 10
4 5 6 7 8 9
9 8 7 6 5 4
7 8 9
6 5 4
1 2 3 10 11 12
12 11 10 3 2 1
Explanation
If you split the given data into 2x3 submatrices and print them in the specified format, you will obtain the output. Let's go over the input processing.
Line 5 i.e. input 1 2 3 asks to print matrices 1, 2 and 3. Output lines 1 and 2 corresponds to this input.
Line 6 i.e. input 4 1 asks to print matrices 4 and 1. Output lines 3 and 4 corresponds to this input.
Similarly processing line number 7, 8 and 9 of the input, we obtain lines the remaining lines of the output.