描述
写一个程序完成以下命令:
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行每行一个命令。
输出
按题目要求输出。
样例输入
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
样例输出
1 2 3
1 2 3 4
1 1 2 2 3 3 4
1 2 3 4
我写的:
#include<iostream>
#include<list>
#include<string>
#include<stdio.h>
using namespace std;
int main()
{
int n, x1, x2;
list<int> a[10001]; //用list定义数组,好神奇
cin >> n;
while(n--)
{
string st;
cin >> st;
if(st == "new") //这里不就相当于什么都没干,提前开好了已经
{
cin >> x1;
}
else if(st == "add")
{
cin >> x1 >> x2;
a[x1].push_back(x2);
}
else if(st == "merge")
{
cin >> x1 >> x2;
a[x1].merge(a[x2]); //在这里wa了好几次,我给他加上了清空函数,结果对,但是wa了
}
else if(st == "unique") //这里记得要先排序
{
cin >> x1;
a[x1].sort();
a[x1].unique();
}
else if(st == "out")
{
cin >> x1;
a[x1].sort();
list<int>::iterator i;
for(i = a[x1].begin(); i != a[x1].end(); i++)
{
cout << *i << " ";
}
cout << endl;
}
}
return 0;
}