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.
这个问题的任务很简单:将一系列不同的正整数插入到哈希表中,并输出输入数字的位置。散列函数定义为H (k e y )= k e y %T S i z e其中T S i z e是哈希表的最大大小。二次探测(仅具有正增量)用于解决冲突。请注意,表格大小最好是素数。如果用户给出的最大大小不是素数,则必须将表大小重新定义为最大素数,该素数大于用户给定的大小。输入规格:每个输入文件包含一个测试用例。对于每种情况,第一行包含两个正数:M S i z e(≤ 1 0 ^ 4)和N(≤ ≤MSize),其分别是用户定义的表大小和输入号的数目。然后下一行给出了N个不同的正整数。一行中的所有数字都用空格分隔。输出规格:对于每个测试用例,在一行中打印输入数字的相应位置(索引从0开始)。一行中的所有数字都用空格分隔,并且行的末尾不能有额外的空格。如果无法插入号码,请打印“ - ”。
Sample Input:
4 4
10 6 4 15
Sample Output:
0 1 4 -
思路详解:主要是二次检测:下列三种,本代码采用第一种;
Hash( i ) = Hash ( 0 ) + i 2;
Hash( i ) = Hash( i - 1) + 2 * i - 1;
Hash( i ) = Hash ( 0 ) + ( i - 1 ) 2;
代码详解
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include <string.h>
#define N 10005
typedef struct HTable *HashTable;
struct HTable
{
int *list;//指针数组
int size;//元素数目大小
};
HashTable CreatTable(int n);
int Insert(HashTable H, int num);
int NextPrime(int n);
int main()
{
int i, m, n, num,k = 0;
int pos[N];
HashTable H;
scanf("%d %d", &m, &n);
H = CreatTable(m);
for (i = 0; i < n; i++)
{
scanf("%d", &num);
pos[k] = Insert(H, num);
k++;
}
printf("%d", pos[0]);
for (i = 1; i < k; i++)
{
if (pos[i] == -1)
printf(" -");
else
printf(" %d", pos[i]);
}
return 0;
}
HashTable CreatTable(int n)
{
HashTable H;
int i;
H = (HashTable)malloc(sizeof(struct HTable));
H->size = NextPrime( n );//寻找下一个最小的素数
H->list = (int*)malloc(H->size*sizeof(int));//指针数组
for (i = 0; i < H->size; i++)
H->list[i] = 0; //初始化
return H;
}
int Insert(HashTable H, int num)
{
int i = 0,pos = num % H->size;
int current = pos;
for(i = 0;i <= H->size - 1&& H->list[pos] ;i++)
pos = (current + (int)pow(i,2) ) % H->size;
if(i >= H->size)//超出范围
return -1;
else
{
H->list[pos] = 1;//标记
return pos; //返回位置
}
}
int NextPrime(int n)
{
int i;
if(n == 2|| n == 1)//最小值的情况
return 2;
if( !(n % 2) ) n++;
while(1)// n>=3的情况
{
for(i = (int)sqrt(n);i >= 3;i--)
if(n % i == 0)
break;
if(i == 2)
break;
else
n += 2;
}
return n;
}