《数据结构》11-散列2 Hashing

题目

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 (≤10​ 4 ^4 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.

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 -

分析

题目大意:对于插入的值,输出其所在位置,如果无法插入,输出"-"
考察散列表
中规中矩的一道散列表题,除留余数法求哈希函数,变化后的平方探测法解决冲突,之所以变化是因为它只有正数,1 2 ^2 2,2 2 ^2 2,3 2 ^2 2,…,⌊TableSize/2⌋ 2 ^2 2

#include<iostream>
#include<cmath>
#include<stdlib.h>
#define MAXTABLESIZE 100000
#define NOTFIND -1
typedef int ElementType;
typedef enum{     // 单元格状态 
	Legitimate,Empty
} EntryType;   // 分别对应:有合法元素和有空位 
typedef struct HashEntry Cell;
struct HashEntry{   // 单元格 
	ElementType data;  // 存值
	EntryType info;   // 存状态 
};
typedef struct HashTbl *HashTable;
struct HashTbl{  // 哈希表
	int TableSize;  // 大小 
	Cell *Cells;   // 数组 
};
using namespace std;

// 除留余数法哈希函数 
int Hash(int key,int p){
	return key%p;
}

// 查找下一个素数 
int NextPrime(int N){
	int p = N%2?N:N+1;
	int i;
	if(N<=2)  
		return 2;
	else if(N<=3)
		return 3; 
	while(p <= MAXTABLESIZE){
		for(i=(int)sqrt(p);i>2;i--)
			if(!(p%i))   // 不是素数 
				break;
		if(i==2)  // 找到了
			break;
		p += 2; 
	}
	return p;
}

// 创建哈希表 
HashTable CreateTable(int TableSize){
	HashTable H;
	H = (HashTable)malloc(sizeof(struct HashTbl));
	H->TableSize = NextPrime(TableSize);
	H->Cells = (Cell *)malloc(sizeof(struct HashEntry)*H->TableSize);
	for(int i=0;i<H->TableSize;i++)
		H->Cells[i].info = Empty;
	return H;	
}

//查找
int Find(HashTable H,ElementType key){
	int NewPos,CurrentPos;
	int CNum = 0;  // 记录冲突次数 
	CurrentPos = NewPos = Hash(key,H->TableSize);
	// 如果当前状态不为空,且一直不等,一直做 
	while(H->Cells[NewPos].info != Empty && H->Cells[NewPos].data != key){
		CNum++;
		NewPos = (CurrentPos + CNum*CNum)%H->TableSize;
		if(CNum == H->TableSize/2) // 没找到
			return NOTFIND;
	}
	return NewPos;
}

int Insert(HashTable H,ElementType key){
	int pos;
	pos = Find(H,key);
	if(pos==NOTFIND) // 如果没找到 
		return NOTFIND;
	else if(H->Cells[pos].info != Legitimate){
		H->Cells[pos].info = Legitimate;
		H->Cells[pos].data = key;
	}
	return pos;
} 

int main(){
	HashTable H;
	int M,N;
	int key;
	cin>>M>>N;
	H = CreateTable(M);
	for(int i=0;i<N;i++){
		cin>>key;
		int pos = Insert(H,key);
		if(i)
			cout<<" ";
		if(pos==NOTFIND)
			cout<<"-";
		else
			cout<<pos;
	}
	return 0;
}

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值