A:List –STL

A:List

总时间限制: 4000ms 内存限制: 65536kB

描述

写一个程序完成以下命令:
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

大概就是list模板练习
注意unique之前要sort
行末有空格?(似乎去掉空格还WA了(还是我写错了qaq

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define inf 1000000007
#define ll long long
#define N 10010
inline int rd()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n;
char op[10];
list<int>a[N];
int main()
{
    n=rd();
    int x,y;
    while(n--){
        scanf("%s",op);
        if(op[0]=='a'){
            x=rd();y=rd();
            a[x].push_back(y);
        }else if(op[0]=='o'){
            x=rd();
            a[x].sort();
            for(list<int>::iterator it=a[x].begin();it!=a[x].end();++it)
                printf("%d ",*it);
            puts("");
        }else if(op[0]=='m'){
            x=rd();y=rd();
            a[x].merge(a[y]);
        }else if(op[0]=='u'){
            x=rd();
            a[x].sort();
            a[x].unique();
        }else x=rd();
    }
    return 0;
}

评论

还没有任何评论,你来说两句吧

发表评论

衫小寨 出品