P4305 [JLOI2011]不重复数字

题目链接:https://www.luogu.com.cn/problem/P4305

方法一:哈希表

#include<bits/stdc++.h>
using namespace std;
const int P=10007;
int t, n;
vector <int> hs[P];
int f(int x){
	return (x%P+P)%P;
}
bool get(int k){
	if(hs[f(k)].size()==0)
		return 0;
	else{
		for(int i=0; i<hs[f(k)].size(); i++)
			if(hs[f(k)][i]==k)return 1;
		return 0;
	}
}
void add(int k){
	hs[f(k)].push_back(k);
}
bool hash_table(int k){
	if(get(k))
		return 0;
	else{
		add(k);
		return 1;
	}	
}

int main()
{
	cin>>t;
	while(t--){
		cin>>n;
		while(n--){
			int x;
			cin>>x;
			if(hash_table(x))
				cout<<x<<" ";
		}
		cout<<endl;
		memset(hs, 0, sizeof(hs));
		//hs[P].clear();
	}

	return 0;
}

方法二:

posted @ 2023-02-18 18:59  TFLSNOI  阅读(32)  评论(0编辑  收藏  举报