用C++main函数实现:Write a program to complete the following command: new id - create a specified sequence of Numbers for id (id < 200000) Add id num - to join the sequence of Numbers for id integer num merge id1 id2 - merger sequence number of id1 and id2, and make id2 empty unique id - remove repetitive element in the sequence id out id - from small to large, output the elements of sequence of Numbers for ids separated by Spaces Input The First Line is n which mean the number of the Command (n <= 200000). The next n lines are correct Command. You don't have to worry wrong input and id is int. Output Then output according the requirements. Sample Input 16 new 1 new 2 add 1 1 add 1 2 add 1 3 add 2 1 add 2 2 add 2 3 add 2 4 out 1 out 2 merge 1 2 out 1 out 2 unique 1 out 1 Sample Ouput 1 2 3 1 2 3 4 1 1 2 2 3 3 4 1 2 3 4
时间: 2024-01-05 08:04:30 浏览: 26
```c++
#include <iostream>
#include <string>
#include <sstream>
#include <set>
using namespace std;
// 定义结构体存储每个id对应的序列
struct Node {
set<int> numbers;
};
int main() {
int n;
cin >> n;
// 定义结构体数组
Node nodes[200000];
// 处理每个命令
for (int i = 0; i < n; i++) {
string cmd, s_id, s_num, s_id1, s_id2;
cin >> cmd;
// 根据空格分离出命令类型和参数
if (cmd == "new") {
cin >> s_id;
int id = stoi(s_id);
nodes[id].numbers.clear(); // 清空序列
}
else if (cmd == "add") {
cin >> s_id >> s_num;
int id = stoi(s_id);
int num = stoi(s_num);
nodes[id].numbers.insert(num); // 添加元素
}
else if (cmd == "merge") {
cin >> s_id1 >> s_id2;
int id1 = stoi(s_id1);
int id2 = stoi(s_id2);
nodes[id2].numbers.insert(nodes[id1].numbers.begin(), nodes[id1].numbers.end()); // 合并序列
nodes[id1].numbers.clear(); // 清空序列
}
else if (cmd == "unique") {
cin >> s_id;
int id = stoi(s_id);
set<int> unique_numbers;
for (int num : nodes[id].numbers) {
unique_numbers.insert(num); // 去重
}
nodes[id].numbers = unique_numbers; // 更新序列
}
else if (cmd == "out") {
cin >> s_id;
int id = stoi(s_id);
for (int num : nodes[id].numbers) {
cout << num << " ";
}
cout << endl;
}
}
return 0;
}
```