zoukankan      html  css  js  c++  java
  • bzoj2761 [JLOI2011]不重复数字

    Description

    给出N个数,要求把其中重复的去掉,只保留第一次出现的数。
    例如,给出的数为1 2 18 3 3 19 2 3 6 5 4,其中2和3有重复,去除后的结果为1 2 18 3 19 6 5 4。
     

    Input

    输入第一行为正整数T,表示有T组数据。
    接下来每组数据包括两行,第一行为正整数N,表示有N个数。第二行为要去重的N个正整数。
     

    Output

     
    对于每组数据,输出一行,为去重后剩下的数字,数字之间用一个空格隔开。

    Sample Input

    2
    11
    1 2 18 3 3 19 2 3 6 5 4
    6
    1 2 3 4 5 6

    Sample Output

    1 2 18 3 19 6 5 4
    1 2 3 4 5 6

    HINT

    对于30%的数据,1 <= N <= 100,给出的数不大于100,均为非负整数;


    对于50%的数据,1 <= N <= 10000,给出的数不大于10000,均为非负整数;


    对于100%的数据,1 <= N <= 50000,给出的数在32位有符号整数范围内。


    提示:


    由于数据量很大,使用C++的同学请使用scanf和printf来进行输入输出操作,以免浪费不必要的时间。


    吉林省选怎么会出如此奇葩的题……这不是让人AK……233

    哈希。写了个类似边表的来记数据。

    ps:此题也可以用treap来判重。只要支持插入就行简单又好写23333333反正我写的是hash

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #define mod 1000007
    using namespace std;
    struct hashdat{
    int next,dat;
    }hash[500000];
    int head[mod];
    int cnt;
    inline int read()
    {
        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;
    }
    inline void ins(int u,int w)
    {
    hash[++cnt].dat=w;
    hash[cnt].next=head[u];
    head[u]=cnt;
    }
    inline bool hashing(int x)
    {
    for (int i=head[(x%mod+mod)%mod];i;i=hash[i].next)if (hash[i].dat==x) return 0;
    ins((x%mod+mod)%mod,x);
    return 1;
    }
    void work()
    {
    int n=read(),len=0;
    int ans[100000];
    memset(hash,0,sizeof(hash));
    memset(head,0,sizeof(head));
    cnt=0;
    for (int i=1;i<=n;i++)
     {
      int a=read();
      if(hashing(a)) ans[len++]=a;
     }
    for (int i=0;i<len;i++)
    {
    printf("%d",ans[i]);
    if (i!=len-1) printf(" ");else printf("
    ");
    }
    }
    int main()
    {
    int t;
    t=read();
    while (t--) work();
    } 


    ——by zhber,转载请注明来源
  • 相关阅读:
    React之Antd组件<Input />属性defaultValue问题记录。
    java多继承问题
    python多继承问题
    React之state
    概念题
    修复linux的yum源
    stl
    java
    python
    各种执行方式
  • 原文地址:https://www.cnblogs.com/zhber/p/4036114.html
Copyright © 2011-2022 走看看