14226 - Band Ya Rou Ze (revisited)   

Description

After catching Jerryfish, 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 three types of moving:

  1. all the people in the ai-th row move to the front of the bi-th row (without messing up the original order)
  2. all the people in the ai-th row move to the back of the bi-th row (without messing up the original order)
  3. exchange all the people in the ai-th row and the bi-th row (without messing up the original order)

Now it’s your turn! You need to help them to move and tell Squidward the final formation in the end.

Input

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 three integer typeiaibi (1 ≤ typei ≤ 3, 1 ≤ aibi ≤ Naibi) – the type of moving and the numbers of two rows which need to move.

 

It's guaranteed that:

  • The 1st testcase must be identical to the Sample below
  • For the first five testcases: 1 ≤ NQ ≤ 103, the sum of all the szi ≤ 104

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] = {};
 
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)
 
    */
}
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] = ...
    ...
    */
}

Output

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.

Sample Input  Download

Sample Output  Download

Tags




Discuss