• Open Judge 3339 List


    3339: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
     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <list>
     5 #include <map>
     6 using namespace std;
     7 
     8 map<int,list<int> > m;
     9 
    10 int n,x,y;
    11 char op[10000];
    12 int main(){
    13     scanf("%d",&n);
    14     while(n--){
    15         scanf("%s",op);
    16         if(strcmp(op,"new")==0){
    17             scanf("%d",&x);
    18             m.insert(map<int,list<int> >::value_type(x,list<int>()));
    19         }
    20         if(strcmp(op,"add")==0){
    21             scanf("%d%d",&x,&y);
    22             m[x].push_back(y);
    23         }
    24         if(strcmp(op,"merge")==0){
    25             scanf("%d%d",&x,&y);
    26             m[x].merge(m[y]);
    27             //m[y].clear();
    28         }
    29         if(strcmp(op,"unique")==0){
    30             scanf("%d",&x);
    31             m[x].sort();
    32             m[x].unique();
    33         }
    34         if(strcmp(op,"out")==0){
    35             scanf("%d",&x);
    36             m[x].sort();
    37             list<int>::iterator it;
    38             for(it=m[x].begin();it!=m[x].end();it++){
    39                 printf("%d ",*it);
    40             }
    41             printf("
    ");
    42         }
    43     }
    44 }

    竟然还有list这STL,长见识了真是~~唉~~

  • 相关阅读:
    SVN版本控制服务
    JVM内存结构
    Git的使用
    Nginx详解
    Apache(httpd)详解
    rsyslog日志收集器
    nsswitch名称解析框架
    NFS网络文件系统
    ThreadLocal详解
    RocketMQ踩坑记
  • 原文地址:https://www.cnblogs.com/suishiguang/p/6158299.html
Copyright © 2020-2023  润新知