算法 散列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​​) 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 -

解答:

首先看题目并不难,基本概念。但是有一点需要注意,我们需要确定什么时候不能插进去的情况。

根据平方探测法,探测次数 n 如果大于等于 TableSize/2,这说明找不到位置了。具体原因这里不探究。

程序代码:

#include <iostream>
#include <cmath>
using namespace std;

typedef struct
HashTbl *HashTable;
#define Empty 0
#define Legitimate 1
typedef unsigned int ElementType;
struct Cell {
	ElementType Element;
	int Info;
};
struct HashTbl {
	int TableSize;
	Cell *TheCells;
}H;
typedef int Position;
#define MAXTABLESIZE 1000000
int NextPrime(int N)
{ // 返回大于N且不超过MAXTABLESIZE的最小素数 
	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; // 否则试探下一个奇数 
	}
	if (N < 2)p = 2;
	if (N>2&&N<=3)p = 3;
	return p;
}
#define MinTableSize 1
HashTable InitializeTable(int TableSize)
{
	HashTable H;
	int i;
	if (TableSize < MinTableSize) {
		//Error("散列表太小");
		return NULL;
	}
	// 分配散列表 
	H = (HashTable)malloc(sizeof(struct HashTbl));
	if (H == NULL)
		;//FatalError("空间溢出!!!");
	H->TableSize = NextPrime(TableSize);
	//cout << H->TableSize << endl;
	// 分配散列表 Cells 
	H->TheCells = (Cell *)malloc(sizeof(Cell)*H->TableSize);
	if (H->TheCells == NULL)
		;//FatalError("空间溢出!!!");
	for (i = 0; i < H->TableSize; i++)
		H->TheCells[i].Info = Empty;
	return H;
}
int Hash(int Key, int P)
{ // 除留余数法法散列函数 
	return Key%P;
}
Position Find(ElementType Key, HashTable H) //平方探测
{
	Position CurrentPos, NewPos;
	int CNum; //记录冲突次数
	CNum = 0;
	NewPos = CurrentPos = Hash(Key, H->TableSize);
	while (H->TheCells[NewPos].Info != Empty &&
		H->TheCells[NewPos].Element != Key) {
		// 字符串类型的关键词需要 strcmp 函数!! 
		NewPos = CurrentPos + (CNum + 1)*(CNum + 1);
		while (NewPos >= H->TableSize)
			NewPos -= H->TableSize;
		CNum++;
		if (CNum == H->TableSize/2)return -1;
	}
	return NewPos;
}
void Insert(ElementType Key, HashTable H,int spaceFlag)
{ // 插入操作 
	Position Pos;
	Pos = Find(Key, H);
	if (H->TheCells[Pos].Info != Legitimate) {
		//确认在此插入
		H->TheCells[Pos].Info = Legitimate;
		H->TheCells[Pos].Element = Key;
		//字符串类型的关键词需要 strcpy 函数!!
		if (spaceFlag == 0)
			if(Pos != -1)
				cout << Pos << " ";
			else
				cout << '-' << " ";
		else
			if (Pos != -1)
				cout << Pos;
			else
				cout << '-';
	}
}

int main()
{
	int M, N;
	cin >> M >> N;
	HashTable myTable = InitializeTable(M);
	int key;
	for (int i = 0; i < N-1;i++) {
		cin >> key;
		Insert(key,myTable,0);
	}
	cin >> key;
	Insert(key, myTable, 1);


	system("pause");
	return 0;
}

测试结果:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dezeming

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值