1078. Hashing (25)

1.题目要求使用二次探测(平方探测)的方法进行哈希。

2.题目中提到MSize的最大值为10000,而比10000大的最小的一个质数为10007,刚开始误以为是10001,卡了一下

3.Quadratic probing即平方探测,公式为h(x)=(Hash(x)+j*j)% MSize,Hash(x)=x%MSize。

4.其他的开放定址方法还有线性探测法,双散列等等。

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. 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 two positive numbers: MSize (<=104) and N (<=MSize) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the corresponding positions (index starts from 0) of the input numbers in one line. All the numbers in a line are separated by a space, and there must be no extra space at the end of the line. In case it is impossible to insert the number, print “-” instead.

Sample Input:

4 4
10 6 4 15

Sample Output:

0 1 4 -

AC代码:

//#include<string>
//#include <iomanip>
//#include<stack>
//#include<unordered_set>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<unordered_map>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include <algorithm>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
#include<stack>
using namespace std;
/*
4 4
10 6 4 15

5 4
10 6 4 15
6 4
10 6 4 15

*/
int main(void)
{
	
	int mSize, n;
	cin >> mSize >> n;
	vector<bool> prime(10008, true);
	prime[0] = false;
	prime[1] = false;
	for (int i = 2; i < prime.size(); i++)
	{//处理质数
		if (prime[i])
		{
			for (int j = 2; j*i < prime.size(); j++)
				prime[j*i] = false;
		}
	}

	if (!prime[mSize])
	{//如果mSize不为质数,则把mSize改为比mSize大的最小质数
		for (int i = mSize + 1; i < prime.size(); i++)
		{
			if (prime[i])
			{
				mSize = i;
				break;
			}
		}
	}
	vector<int> num(n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &num[i]);
	}
	vector<bool> exist(mSize, false);
	vector<int> position(n, -1);
	for (int i = 0; i < n; i++)
	{
		int pos = num[i] % mSize;//直接哈希地址
		for (int j = 0; j < mSize; j++)
		{//二次探测
			int tmpPos = (pos + j*j) % mSize;
			if (!exist[tmpPos])
			{//如果有空位
				exist[tmpPos] = true;
				position[i] = tmpPos;
				break;
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		if (position[i] != -1)
			printf("%d", position[i]);
		else
			printf("-");
		if (i != n - 1)
			printf(" ");
	}
	cout << endl;
	return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注