题目链接:https://www.luogu.com.cn/problem/P4305
分析:
由于数字比较大,无法用下标来标识数字是否出现过,因此会想到map,但是map底层基于红黑树,对于这道题会超限,可以使用unordered_map,底层基于hash,不过这里要加个输入外挂,下面代码手打hash。
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
#define ll long long
const int N = 1e6;
const int P = 49993;
vector<int> v[N];
// 输入输出外挂
template <class T>
inline bool scan_d(T &ret)
{
char c; int sgn;
if(c = getchar(),c == EOF) return 0; //EOF
while(c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while(c = getchar(),c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
// 获得hash值
int hash_code(int x)
{
return (x % P + P) % P;
}
// 判断是否能够插入
bool insert(int x)
{
int key = hash_code(x);
for(int i = 0; i < v[key].size(); i++)
if(v[key][i] == x) return false;
v[key].push_back(x);
return true;
}
int main()
{
int T,n;
scanf("%d",&T);
while(T--) {
scan_d(n);
int x;
for(int i = 0; i < N; i++) v[i].clear();
for(int i = 0; i < n; i++) {
scan_d(x);
if(insert(x)) printf("%d ",x);
}
printf("\n");
}
return 0;
}