[BZOJ2761][JLOI2011]不重复数字

[BZOJ2761][JLOI2011]不重复数字

试题描述

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

输入

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

输出

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

输入示例

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

输出示例

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

数据规模及约定

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

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

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

题解

直接上哈希。。。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std;

int read() {
    int x = 0, f = 1; char c = getchar();
    while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
    while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
    return x * f;
}

#define MOD 100007
#define LL long long
int head[MOD], nxt[MOD], ans[MOD], cnt, val[MOD], ToT;
void insert(int x) {
	int p = (x % MOD + MOD) % MOD;
	nxt[++ToT] = head[p]; head[p] = ToT; val[ToT] = x;
	return ;
}
bool Find(int x) {
	int p = (x % MOD + MOD) % MOD;
	for(int i = head[p]; i; i = nxt[i]) if(x == val[i]) return 1;
	return 0;
}

int main() {
	int T = read();
	while(T--) {
		int n = read(); cnt = ToT = 0;
		memset(head, 0, sizeof(head));
		for(int i = 1; i <= n; i++) {
			int x = read();
			if(!Find(x)) insert(x), ans[++cnt] = x;
		}
		for(int i = 1; i <= cnt; i++) printf("%d%c", ans[i], i < cnt ? ' ' : '\n');
	}
	
	return 0;
}

 

posted @ 2016-12-20 21:49  xjr01  阅读(291)  评论(0编辑  收藏  举报