1145 Hashing - Average Search Time (25 分)(散列冲突检测)

1145 Hashing - Average Search Time (25 分)

The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of integer keys from the table and output the average search time (the number of comparisons made to find whether or not the key is in the table). The hash function is defined to be H(key)=key%TSize where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.

Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.

Input Specification:

Each input file contains one test case. For each case, the first line contains 3 positive numbers: MSize, N, and M, which are the user-defined table size, the number of input numbers, and the number of keys to be found, respectively. All the three numbers are no more than 10​4​​. Then N distinct positive integers are given in the next line, followed by M positive integer keys in the next line. All the numbers in a line are separated by a space and are no more than 10​5​​.

Output Specification:

For each test case, in case it is impossible to insert some number, print in a line X cannot be inserted. where X is the input number. Finally print in a line the average search time for all the M keys, accurate up to 1 decimal place.

Sample Input:

4 5 4
10 6 4 15 11
11 4 15 2

Sample Output:

15 cannot be inserted.
2.8

题目大意:

先给一行数,建立一个散列,用平方冲突检测法检测冲突,只取正的。然后给定一行数,计算平均查找时间。

思路:

1、对于正负的平方冲突散列,检测到q<=(tablesize/2)的平方,对于只取正的,检测到q=tablesize的平方。其实对于正的,个人认为只需要检测到q=(tablesize-1)的平方就够了,因为一个下标,加上tablesize的平方,还是这个下边,一点没变。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.HashMap;
import java.util.Map;

public class Main {

	static int Msize,N,M;
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		in.nextToken();
		Msize = (int)in.nval;
		in.nextToken();
		N = (int) in.nval;
		in.nextToken();
		M = (int)in.nval;
		int size = prime(Msize);
		int map[] = new int [size];
		int flag=1;
		Map<Integer,Integer> hm = new  HashMap<>();
		for(int i=0;i<N;i++) {
			in.nextToken();
			int num = (int)in.nval;
			for(int j=0;j<=size;j++) {
				int index = (num+j*j)%size;
				if(map[index]==0) {
					hm.put(num, j+1);
					map[index]=num;
					flag=0;
					break;
				}
			}
			if(flag==1) {
				System.out.println(num+" cannot be inserted.");
				
			}
			flag=1;
		}
		
		int sum=0;
		for(int i=0;i<M;i++) {
			in.nextToken();
			int num = (int)in.nval;
			if(hm.get(num)!=null) {
				sum+=hm.get(num);
			}
			else {
				int bool=0;
				for(int j=0;j<=size;j++) {
					int index = (num+j*j)%size;
					if(map[index]==0) {
						sum+=j+1;
						bool=1;
						break;
					}
				}
				if(bool==0) {
					sum+=size+1;
				}
			}
		}
		
		double aver = (double)sum/M;
		System.out.printf("%.1f",aver);
		
		
		
	}
	private static int prime(int msize) {
		// TODO Auto-generated method stub
		if(msize==1||msize==2)
			return 2;
		for(int num = msize;;num++) {
			int flag=1;
			for(int i=2;i<Math.sqrt(num)+1;i++) {
				if(num%i==0) {
					flag=0;
					break;
				}
			}
			if(flag==1)
				return num;
		}
	}

}

猜你喜欢

转载自blog.csdn.net/weixin_38902950/article/details/88061324