After catching Suicune, Spongebob and Patrick thought they were happy enough so they went home. And then they found that Squidward had come home already (:
Squidward told them he need to drum up a marching band fast(for some reason). So he needed their help. Hence Spongebob and Patrick joined Squidward's band.
Today is their practicing day. Squidward is teaching all the members how to move at a performance. At a performance there are N rows. And in the i-th row, there are szi people. Note row may be empty(i.e. szi = 0). Everyone plays exactly one musical instrument. The musical instrument played by the j-th people in the i-th row is represented by a lower case letter cij.
Squidward ask them to move Q times. In each move, there are four types of moving:
Now it’s your turn! You need to help them to move and tell Squidward the final formation in the end.
Hint for reverse:
1. Easy way , but you can only get 2 more correct answers.
But, you should think another way to accelerate your performance to get AC.
Hint:
If you have no idea how to implement, please take it as your reference.
#include <stdlib.h> #include <stdio.h> typedef struct _Node { char val; struct _Node* next; } Node; //list[i]'s head node Node *head[100005] = {}; //list[i]'s tail node Node *tail[100005] = {}; //reverse(list[i])'s head node Node *rev_head[100005] = {}; //reverse(list[i])'s tail node Node *rev_tail[100005] = {}; /* list[a] = 1 -> 2 -> 3 -> 4 rev_list[a] = 4 -> 3 -> 2 -> 1 head[a]->val = 1, tail[a]->val = 4 rev_head[a]->val = 4, rev_tail[a]->val = 1 you can use rev_head and rev_tail to get O(1) reverse */ void swap(int a, int b) { //swap list[a] and list[b] Node *tmp = (Node *)malloc(sizeof(Node)); /* swap(head_node) tmp = head[a]; head[a] = head[b]; head[b] = tmp; */ /* swap(tail_node) */ /*swap(rev_head) */ /*swap(rev_tail) */ } void append(int a, int b) { //append list[a] to list[b]'s behind if(head[a] == NULL) return; if(head[b] == NULL) { swap(a, b); return; } /* tail[b]->next = head[a]; tail[b] = ... ... */ } void reverse(int a) { //reverse list[a] if(head[a] == NULL) return; /* use rev_head and rev_tail to reverse list[a] in O(1) hint: swap something */ }
The first line contains one integer N (1 ≤ N ≤ 105) – the number of rows.
Then N lines follow. First the i+1-th line contains one integer szi – the number of people in the i-th row. If there are some people in the row, a space character and szi characters cij are followed where cij is the musical instrument played by the j-th people in the i-th row. The sum of all the szi is less than or equal to 106.
The N+2-th line contains one integer Q (1 ≤ Q ≤ 105) – the times they need to move.
Then Q lines follow. The i+N+3-th line contains two integer typei, ai. If typei isn't equal to 4, then a integer bi follows(1 ≤ typei ≤ 4, 1 ≤ ai, bi ≤ N, ai ≠ bi) – they are the corresponding information for each moving.
It's guaranteed that:
After all the moving, for each row output all the musical instrument played in that row and then print a newline('\n') character.
If there is no people in some row, just output an empty line.
Note: there are two sample below. "# Sample Input 1/2" and "# Sample Output 1/2" are not the part of input and output.
They are just for marking that the following content corresponds to which sample.