zoukankan      html  css  js  c++  java
  • Openjudge 3.9-3339

    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
    深刻了解STL的无敌。。自己手打的不是TLE就是RE
    满分代码
    #include<bits/stdc++.h>
    using namespace std;
    
    int n,m,k,l,s,t,r;
    list<int>a[200005];
    string p;
    
    int main()
    {
        //freopen("1.txt","r",stdin);
        int i,j;
        ios::sync_with_stdio(false);
        cin>>n;
        for(j=1;j<=n;j++)
        {
            cin>>p;
            if(p=="new") cin>>l;
            else    
            if(p=="add")
            {
                cin>>l>>r;
                a[l].push_back(r);    
            }    
            else if(p=="out")    
            {
                cin>>l;
                if(a[l].empty())
                cout<<endl;    
                else 
                {
                    a[l].sort();
                    for(list<int> :: iterator i=a[l].begin();i!=a[l].end();i++)
                    cout<<*i<<" ";
                    cout<<endl;    
                }    
            }    
            else if(p=="merge")    
            {
                cin>>l>>r;    
                a[l].merge(a[r]);
            }
            else
            if(p=="unique")
            {
                cin>>l;
                a[l].sort();    
                a[l].unique();    
            }    
        }
        return 0;
    }
    点击展开

      手写代码 (0分仅供参考)

    #include <vector>
    #include <string>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    struct node{
        int a[20000];
        int w;
    }e[10000];
    string a;
    vector<int>q;
    bool s[200001],d[200001];
    int f,b[200001],n,i,id,j,r,z,y;
    int main()
    {
        cin>>n;
        while(n--)
        {
            cin>>a;
            if(a=="new") {
                cin>>r;
                d[r]=1;
            }
            if(a=="add") {
                cin>>z>>y;
                e[z].a[e[z].w++]=y;
            }
            if(a=="out") {
                cin>>r;
                if(d[r]==1)
                {
                    for(i=0;i<e[r].w;++i)
                    if(e[r].a[i]!=0) b[i]=e[r].a[i];
                    sort(b,b+e[r].w);
                    for(i=0;i<e[r].w;++i)
                    cout<<b[i]<<" ";
                }
                cout<<endl;
            }
            if(a=="merge")
            {
                cin>>z>>y;
                for(i=0;i<e[y].w;++i)
                e[z].a[e[z].w++]=e[y].a[i];
                d[y]=0;
            }
            if(a=="unique")
            {
                memset(s,0,sizeof(s));
                cin>>r;
                for(i=0;i<e[r].w;++i)
                if(s[e[r].a[i]]==0)    
                {
                    q.push_back(e[r].a[i]); 
                    s[e[r].a[i]]=1;
                }
                e[r].w=0;
                while(!q.empty())
                {
                    e[r].a[e[r].w++]=q.back() ;
                    q.pop_back();
                }
            }
        }
    }
    点击展开

              普及点知识。 

              C++ list的基本操作和使用

    Lists将元素按顺序储存在链表中. 与 向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢.


      assign() 给list赋值
      back() 返回最后一个元素
      begin() 返回指向第一个元素的迭代器
      clear() 删除所有元素
      empty() 如果list是空的则返回true
      end() 返回末尾的迭代器
      erase() 删除一个元素
      front() 返回第一个元素
      get_allocator() 返回list的配置器
      insert() 插入一个元素到list中
      max_size() 返回list能容纳的最大元素数量
      merge() 合并两个list
      pop_back() 删除最后一个元素
      pop_front() 删除第一个元素
      push_back() 在list的末尾添加一个元素
      push_front() 在list的头部添加一个元素
      rbegin() 返回指向第一个元素的逆向迭代器
      remove() 从list删除元素
      remove_if() 按指定条件删除元素
      rend() 指向list末尾的逆向迭代器
      resize() 改变list的大小
      reverse() 把list的元素倒转
      size() 返回list中的元素个数
      sort() 给list排序
      splice() 合并两个list
      swap() 交换两个list
      unique() 删除list中重复的元素

      这题就是一个模板题 可以一做。查看

    我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。
  • 相关阅读:
    基于MATLAB求解矩阵的正交补矩阵
    MySQL的安装与配置
    删除ubuntu后修复win7系统的引导
    VS2010中快捷添加命名空间
    java学习之函数
    java学习之break 和 continue
    For循环复杂练习
    For循环练习之99乘法表和转义字符
    java学习之语句结构
    java学习之运算符
  • 原文地址:https://www.cnblogs.com/ruojisun/p/6160016.html
Copyright © 2011-2022 走看看