This is a partial judge problem OwO
Here is an idea to solve 13136 - Band Ya Rou Ze - Reverse: For each row, using a linked list to record all the people. And for each node just record the adjacent node(s) of it rather than the next node and/or the previous node. By using this kind of linked list, all types of moving could be implement in .
In this problem, you need to complete the code which implements the above idea with C++ class. A little difference with 13136 - Band Ya Rou Ze - Reverse is that there are tasks in this problem and the constraint of number of people is changed.
In the following code, nullptr is just like NULL in C. You could treat them as the same.
But nullptr is only available in C++11 and beyond. So compiling/submitting your code in C++11 or beyond.
#include <iostream> #include <string> #include "function.h" using namespace std; int main() { /* I/O optimization */ ios::sync_with_stdio(0); cin.tie(0); int T; cin >> T; while(T--) { int n; cin >> n; List *row = new List[n + 1]; for(int i=1;i<=n;i++) { int sz; cin >> sz; if(sz > 0) { string s; cin >> s; row[i].init(s); } } int q; cin >> q; while(q--) { int type, a, b; cin >> type >> a; if(type != 4) cin >> b; if(type == 1) { row[b].merge(row[a], row[b]); row[a].clear(); } else if(type == 2) { row[b].merge(row[b], row[a]); row[a].clear(); } else if(type == 3) { row[a].swap(row[b]); } else { row[a].reverse(); } } for(int i=1;i<=n;i++) row[i].output(); delete[] row; } return 0; }
#ifndef FUNCTION_H #define FUNCTION_H class Node { private: char instrm; Node *neighbors[2]; /* disconnect the link with a specific node */ void unLink(Node *delNeighbor) { /* you could consider "nullptr" as "NULL" */ if(neighbors[0] == delNeighbor) neighbors[0] = nullptr; if(neighbors[1] == delNeighbor) neighbors[1] = nullptr; } public: Node() {} Node(char instrument) : instrm(instrument), neighbors{nullptr, nullptr} {} /* connect this node with another node */ void link(Node *newNeighbor) { neighbors[(neighbors[0] == nullptr ? 0 : 1)] = newNeighbor; } void output(Node *prevNode) { std::cout << instrm; for(int i=0;i<2;i++) if(neighbors[i] != nullptr && neighbors[i] != prevNode) neighbors[i]->output(this); } /* TODO: destructor for this class */ ~Node(); }; class List { private: Node *head, *tail; // point to the first and the last node of this linked list public: List() : head(nullptr), tail(nullptr) {} /* TODO: using a string to initialize this linked list */ void init(std::string s); /* TODO: make this linked list be a new linked list which is merged from two linked list */ void merge(List &front, List &back); void clear() { head = tail = nullptr; } /* TODO: exchange this linked list with another linked list; try to use std::swap! */ void swap(List &swapList); /* TODO: reverse the order of this linked list */ void reverse(); void output() { if(head != nullptr) head->output(nullptr); std::cout << '\n'; } /* TODO: destructor for this class */ ~List(); }; #endif
The very first line contains one integer – the number of tasks.
The first line of each task contains one integer – the number of rows.
Then lines follow. The
-th line of each task first contains one integer
– the number of people in the
-th row. If there are some people in the row, a space character and
characters cij are followed where
is the musical instrument played by the
-th people in the
-th row. The sum of all the
in one task is less than or equal to
.
The next line contains one integer – the times they need to move.
Then lines follow. The
-th line of each task contains two integer
. If
isn't equal to 4, then a integer
follows
– they are the corresponding information for each moving.
It's guaranteed that:
For each task, output one line which represents all the musical instrument played in every row after all the moving.
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.