11-散列2 Hashing (25分)

11-散列2 Hashing (25分)

原题链接

题目要求利用散列函数(H(key)=key%TSize)来建立散列表,其中冲突解决方法是只有正向的平方探测法。要求输出每个数字在表中的位置(位置从0开始定义),数字间用空格隔开,最后不能有空格,如果不能输入,则用“-”代替。

难点

  1. 如何判断一个数字无法插入(平方探测,每次的值为1^2, 2^2, 3^2, ……,TableSize^2, ……,(TableSize+i)^2……,而当探测值大于TableSize ^2时,对TableSize取余的值为 i ^2,即开始重复探测,此时可以判断无法插入,即冲突次数大于TableSize)
  2. 如果用户输入的M为1,需要返回哈希表大小为2,不需要从下一个奇数开始找

输入中第一行所给出的两个数字——

  • 第一个数字是用户所给出的散列表大小M(需要在程序中重新定义为比这个大的第一个素数)
  • 第二个数字是要输入的数字个数N(接下来的一行会给出N个要输入的数字,其中用空格隔开)

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.

输入格式:
Each input file contains one test case. For each case, the first line contains two positive numbers: MSize (≤10​4​​ ) 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.

输出格式:
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.

输入样例:

4 4
10 6 4 15

输出样例:

0 1 4 -

实现代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define MAXTABLESIZE 100000 /* 允许开辟的最大散列表长度 */
typedef int ElementType;    /* 关键词类型用整型 */
typedef int Index;          /* 散列地址类型 */
typedef Index Position;     /* 数据所在位置与散列地址是同一类型 */
 
typedef struct TblNode *HashTable; /* 散列表类型 */
struct TblNode {   /* 散列表结点定义 */
    int TableSize; /* 表的最大长度 */
    ElementType *data;   /* 存放散列单元数据的数组 */
};
 
int NextPrime( int N )
{ 
	if(N == 1) return 2;
    int i, p = (N%2)? N+2 : N+1; /*从大于N的下一个奇数开始 */
 
    while( p <= MAXTABLESIZE ) {
        for( i=(int)sqrt(p); i>2; i-- )
            if ( !(p%i) ) break; /* p不是素数 */
        if ( i==2 ) break; /* for正常结束,说明p是素数 */
        else  p += 2; /* 否则试探下一个奇数 */
    }
    return p;
}
 
HashTable CreateTable( int TableSize )
{
    HashTable H;
    int i;
 
    H = (HashTable)malloc(sizeof(struct TblNode));
    H->TableSize = NextPrime(TableSize);
    H->data = (int*)malloc(H->TableSize*sizeof(int));
    for( i=0; i<H->TableSize; i++ )
        H->data[i] = -1;
 
    return H;
}

Position Find( HashTable H, ElementType Key )
{
    Position CurrentPos, NewPos;
    int CNum = 0; /* 记录冲突次数 */
    NewPos = CurrentPos = Key % H->TableSize; /* 初始散列位置 */
    while(1){
    	if(H->data[NewPos]==-1){
    		H->data[NewPos] = Key;
    		return NewPos;
		}else{
			CNum++;
			if(CNum > H->TableSize) return -1;
			NewPos = (CurrentPos + CNum*CNum) % H->TableSize;
		}
	}
}


int main()
{
	int M, N, i, tmp;
	int Pos;
	HashTable H;
	scanf("%d %d", &M, &N);
	H = CreateTable(M);
	
	for(i=0; i<N; ++i){
		scanf("%d", &tmp);
		Pos = Find(H, tmp);
		if(Pos == -1) printf("-");
		else printf("%d", Pos);
		if(i != N-1) printf(" ");
		else printf("\n");
	}
	
	return 0;
}

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值