第一次写hash。http://www.vvbin.com/?p=376。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define rep(i,n) for(int i=1;i<=n;i++)
#define clr(x,c) memset(x,c,sizeof(x))
int read(){
int x=0;char c=getchar();bool f=true;
while(!isdigit(c)){
if(c=='-') f=false;c=getchar();
}
while(isdigit(c)) x=x*10+c-'0',c=getchar();
return f?x:-x;
}
const int nmax=50005;
const int inf=0x7f7f7f7f;
const int mod=10007;
bool hash[mod];
int head[mod],cnt,next[nmax],a[nmax],n;
bool find(int x){
int y=(x>=0?x:-x)%mod;
if(!hash[y]) return false;
for(int i=head[y];i;i=next[i]) if(a[i]==x) return true;
return false;
}
void insert(int x){
int y=(x>=0?x:-x)%mod;
hash[y]=true;a[++cnt]=x;
next[cnt]=head[y];head[y]=cnt;
}
int main(){
int t=read();
while(t--){
cnt=0;clr(head,0);clr(next,0);clr(hash,0);
n=read();bool f=false;
rep(i,n) {
int x=read();
if(find(x)) continue;
insert(x);
if(f) printf(" %d",x);else printf("%d",x),f=true;
}
printf("
");
}
return 0;
}
2761: [JLOI2011]不重复数字
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3736 Solved: 1378
[Submit][Status][Discuss]
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
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
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来进行输入输出操作,以免浪费不必要的时间。