手写Hash表最后四个点TLE求指点
查看原帖
手写Hash表最后四个点TLE求指点
647347
yushen6474楼主2024/3/20 18:52
#include<iostream>
#include<stdio.h>
#include<limits.h>

using namespace std;

const int N=100043;

long long hash[100043];

int arr[50010];

int T;
int n;

int find(int x){
	
	int cur=(cur%N+N)%N;
	
	while(hash[cur]!=INT_MAX&&hash[cur]!=x){
		cur++;
		if(cur==N){
			cur=0;
		}
	}
	return cur;
}

void init(){
	for(int i=0;i<100043;i++){
		hash[i]=INT_MAX;
	}
}

int main(){
	
	
	//寻找质数 
//	for(int i=N;;i++){
//		bool a=true;
//		for(int j=2;j<(int)sqrt((double)i);j++){
//			if(i%j==0){
//				a=false;
//				break;
//			}
//		}
//		if(a){
//			cout<<i<<endl;
//			break;
//		}
//	}
	cin>>T;

	while(T--){
		cin>>n;
		
		init();
		for(int i=0;i<n;i++){
			
			scanf("%d",&arr[i]);
			int index=find(arr[i]);
			if(hash[index]==INT_MAX){
				hash[index]=arr[i];
				printf("%d ",arr[i]);
			}
			
		}
		
		
		cout<<endl;
		
	}
	
	return 0;
}
2024/3/20 18:52
加载中...