48 lines
1.2 KiB
C++
48 lines
1.2 KiB
C++
#include <bits/stdc++.h>
|
||
|
||
using namespace std;
|
||
int t;//数据组数
|
||
int 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
|
||
*/
|
||
const int MOD = 1000003;
|
||
vector<int> linker[MOD + 10];//这相当于一个二维动态数组,一维是固定的,二维是动态的.一维对应着HASH后的值,二维是一个链表
|
||
int x; //每次读入的数字
|
||
|
||
//检查是不是出现过
|
||
bool insert(int val) {
|
||
int pos = (val % MOD + MOD) % MOD;//可以处理负数!如此处理,负数也要散列到正数的范围内!
|
||
for (int i = 0; i < linker[pos].size(); i++)
|
||
if (linker[pos][i] == val) return true;
|
||
//没有存在过,加进去
|
||
linker[pos].push_back(val);
|
||
return false;
|
||
}
|
||
|
||
int main() {
|
||
//不用scanf,只使用cin,会TLE掉后4个点
|
||
scanf("%d", &t);
|
||
while (t--) {
|
||
scanf("%d", &n);
|
||
//初始化
|
||
memset(linker, 0, sizeof linker);
|
||
//一般读入,一边判断是否出现过
|
||
for (int i = 1; i <= n; i++) {
|
||
scanf("%d", &x);
|
||
if (!insert(x)) printf("%d ", x);
|
||
}
|
||
printf("\n");
|
||
}
|
||
return 0;
|
||
} |