题目
给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义的散列函数H(Key)将关键字Key中的最后3个字符映射为整数,每个字符占5位;再用除留余数法将整数映射到长度为P的散列表中。例如将字符串AZDEG插入长度为1009的散列表中,我们首先将26个大写英文字母顺序映射到整数0~25;再通过移位将其映射为3×322+4×32+6=3206;然后根据表长得到,即是该字符串的散列映射位置。
发生冲突时请用平方探测法解决。
输入格式:
输入第一行首先给出两个正整数N(≤500)和P(≥2N的最小素数),分别为待插入的关键字总数、以及散列表的长度。第二行给出N个字符串关键字,每个长度不超过8位,其间以空格分隔。
输出格式:
在一行内输出每个字符串关键字在散列表中的位置。数字间以空格分隔,但行末尾不得有多余空格。
输入样例1:
4 11
HELLO ANNK ZOE LOLI
输出样例1:
3 10 4 0
输入样例2:
6 11
LLO ANNA NNK ZOJ INNK AAA
输出样例2:
3 0 10 9 6 1
解法
思路
- 因为此题的输入数据是字符串,所以需要定义数据类型为EleType。用EleType a即可存储输入数据。
#define MAXL 8
typedef char EleType[MAXL+1];
- 整个哈希表涉及的操作有:创建表,查找关键词,插入关键词。这里的散列函数HashFunction比整数型的要复杂一些,具体计算原理见下一点
- HashFunction中应该首先计算key的字符个数,然后计算其最后三个字符相对于字符A的差值,再逐步进行移位操作(当然严格地来说做一个字符个数的判断,少于三个字符的key直接抛弃,但是因为题目会默认大于等于3,所以这里也没判断了),最后对Pos求余。
- 凡是涉及比较,用strcmp,凡是涉及赋值,用strcpy
实现一
实现一是把每个字符串的长度也存在哈希表里了,但是这个不是必要的。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXL 8
typedef char EleType[MAXL+1];
typedef enum{legitimate, empty, deleted} EntryType;
typedef struct HashEntry Cell;
struct HashEntry
{
EleType Data;
EntryType Info;
int Length;
};
typedef struct TblNode *HashTable;
struct TblNode
{
int TableSize;
Cell *Cells;
};
HashTable CreateTable(int TableSize)
{
HashTable H = (HashTable)malloc(sizeof(struct TblNode));
H->TableSize = TableSize;
H->Cells = (Cell *)malloc(H->TableSize*sizeof(Cell));
int i;
for(i=0; i<H->TableSize; i++)
{
H->Cells[i].Data[0] = '\0';
H->Cells[i].Info = empty;
H->Cells[i].Length = 0;
}
return H;
}
int HashFunction(EleType key, int*Length, int TableSize)
{
int i,tmpLength;
unsigned int Pos = 0;
tmpLength = 0;
while(key[tmpLength] != '\0')
tmpLength++;
*Length = tmpLength;
if(tmpLength>2)
{
for(i=tmpLength-3; i<tmpLength; i++)
Pos = (Pos<<5) + (key[i]-'A');
Pos = Pos % TableSize;
}
return Pos;
}
int SqFind(HashTable H, EleType key, int *Length)
{
int CurPos, NewPos;
Length = 0; //Length is necessary?
CurPos = NewPos = HashFunction(key, &Length, H->TableSize);
int count = 0;
while(H->Cells[NewPos].Info!=empty && strcmp(H->Cells[NewPos].Data, key))
{
if(++count %2)
{
NewPos = CurPos + (count+1)*(count+1)/4;
if(NewPos >= H->TableSize)
NewPos = NewPos % H->TableSize;
}
else
{
NewPos = CurPos - (count)*(count)/4;
while(NewPos < 0)
NewPos = NewPos + H->TableSize;
}
}
return NewPos;
}
void Insert(HashTable H, EleType key)
{
int StrLen = 0;
int Pos = SqFind(H, key, &StrLen);
if(H->Cells[Pos].Info != legitimate)
{
strcpy(H->Cells[Pos].Data, key);
H->Cells[Pos].Info = legitimate;
H->Cells[Pos].Length = StrLen;
}
}
int main()
{
int N,P;
scanf("%d %d", &N, &P);
HashTable H = CreateTable(P);
EleType key;
int i,Pos,len,flag;
flag = 0;
for(i=0; i<N; i++)
{
scanf("%s", key);
Insert(H, key);
Pos = SqFind(H, key, &len);
if(flag == 0)
{
printf("%d", Pos);
flag = 1;
}
else
printf(" %d", Pos);
}
return 0;
}
实现二
实现二在哈希表里就不考虑字符串的长度了
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXL 8
typedef char EleType[MAXL+1];
typedef enum{legitimate, empty, deleted} EntryType;
typedef struct HashEntry Cell;
struct HashEntry
{
EleType Data;
EntryType Info;
};
typedef struct TblNode *HashTable;
struct TblNode
{
int TableSize;
Cell *Cells;
};
HashTable CreateTable(int TableSize)
{
HashTable H = (HashTable)malloc(sizeof(struct TblNode));
H->TableSize = TableSize;
H->Cells = (Cell *)malloc(H->TableSize*sizeof(Cell));
int i;
for(i=0; i<H->TableSize; i++)
{
H->Cells[i].Data[0] = '\0';
H->Cells[i].Info = empty;
}
return H;
}
int HashFunction(EleType key, int TableSize)
{
int i,tmpLength;
unsigned int Pos = 0;
tmpLength = 0;
while(key[tmpLength] != '\0')
tmpLength++;
if(tmpLength>2)
{
for(i=tmpLength-3; i<tmpLength; i++)
Pos = (Pos<<5) + (key[i]-'A');
Pos = Pos % TableSize;
}
return Pos;
}
int SqFind(HashTable H, EleType key)
{
int CurPos, NewPos;
CurPos = NewPos = HashFunction(key, H->TableSize);
int count = 0;
while(H->Cells[NewPos].Info!=empty && strcmp(H->Cells[NewPos].Data, key))
{
if(++count %2)
{
NewPos = CurPos + (count+1)*(count+1)/4;
if(NewPos >= H->TableSize)
NewPos = NewPos % H->TableSize;
}
else
{
NewPos = CurPos - (count)*(count)/4;
while(NewPos < 0)
NewPos = NewPos + H->TableSize;
}
}
return NewPos;
}
void Insert(HashTable H, EleType key)
{
int Pos = SqFind(H, key);
if(H->Cells[Pos].Info != legitimate)
{
strcpy(H->Cells[Pos].Data, key);
H->Cells[Pos].Info = legitimate;
}
}
int main()
{
int N,P;
scanf("%d %d", &N, &P);
HashTable H = CreateTable(P);
EleType key;
int i,Pos,len,flag;
flag = 0;
for(i=0; i<N; i++)
{
scanf("%s", key);
Insert(H, key);
Pos = SqFind(H, key);
if(flag == 0)
{
printf("%d", Pos);
flag = 1;
}
else
printf(" %d", Pos);
}
return 0;
}