//http://c...content-available-to-author-only...e.cn/stlpractice2015/A/
/*
描述
写一个程序完成以下命令:
new id ——新建一个指定编号为id的序列(id<10000)
add id num——向编号为id的序列加入整数num
merge id1 id2——合并序列id1和id2中的数,并将id2清空
unique id——去掉序列id中重复的元素
out id ——从小到大输出编号为id的序列中的元素,以空格隔开
输入
第一行一个数n,表示有多少个命令( n<=200000)。以后n行每行一个命令。
输出
按题目要求输出。
*/
#include <iostream>
using namespace std;
#include <cstdio>
#include <string>
#include <vector>
#include <list>
const int MAX = 255;
void getPara(const string &str,int para[3]);
size_t getIndex(vector<int> &list_index,int &var);
void outputElements(list<int> &list0);
int main(void)
{
int n;
int index;
string func;
vector<list<int> > list_vec;
vector<int> list_index;
(cin >> n).get();
for(index = 0;index < n;index++)
{
int para[3] = {0};
func.clear();
getline(cin,func);
//new 1
if(func.find("new")!= string::npos)
{
getPara(func,para);
list<int> new_list;
list_vec.push_back(new_list);
list_index.push_back(para[0]);
continue;
}
//out 1
if(func.find("out")!= string::npos)
{
getPara(func,para);
list<int> temp_list(list_vec[getIndex(list_index,para[0])]);
if(!temp_list.size())
{
cout << endl;
continue;
}
else
outputElements(temp_list);
continue;
}
//add 1 1
if(func.find("add")!= string::npos)
{
getPara(func,para);
list_vec[getIndex(list_index,para[0])].push_back(para[1]);
continue;
}
//merge 1 2
//merge(list &list0):will remove all elements in list0
//both containers shall already be ordered
if(func.find("merge")!= string::npos)
{
size_t j = 0;
size_t list_id[3] = {0};
getPara(func,para);
for(;j < list_index.size();j++)
{
if(list_index[j] == para[0])
list_id[0] = j;
if(list_index[j] == para[1])
list_id[1] = j;
}
list_vec[list_id[0]].sort();
list_vec[list_id[1]].sort();
list_vec[list_id[0]].merge(list_vec[list_id[1]]);
continue;
}
//unique 1
if(func.find("unique")!= string::npos)
{
getPara(func,para);
list_vec[getIndex(list_index,para[0])].unique();
continue;
}
}
return 0;
}
void getPara(const string &str,int para[])
{
int i = 0;
int num = 0;//Indicate the number of parameters
char buf[MAX];
while ( str.c_str()[i] != '\0' )
{
if (isdigit(str[i]))
{
buf[i] = str[i];
num++;
}
else buf[i] = ' ';
++i;
}
buf[i] = '\0';
if(num == 2)//Two parameters,such as:add 1 1
sscanf(buf,"%d %d", ¶[0], ¶[1]);
else
sscanf(buf,"%d",¶[0]);
}
size_t getIndex(vector<int> &list_index,int &var)
{
size_t j = 0;
for(;j < list_index.size();j++)
{
if(list_index[j] == var)
return j;
}
return -1;
}
void outputElements(list<int> &list0)
{
list<int>::iterator it;
for(it = list0.begin();it != list0.end();it++)
{
cout << *it << " ";
}
cout << endl;
}