题目背景
查找的方法有顺序查找、折半查找、索引查找、哈希查找等。其中哈希查找方法对关键字计算其在表中的存放地址,根据地址找到对应的记录,在查找的过程中,可能存在冲突,需要有解决冲突的方法。本题先构建存放单词的哈希表,然后对单词进行查找。
题目描述
从标准输入中读入一个英文单词,在一个给定的英文常用单词字典中查找该单词,返回查找结果(查找到返回1,否则返回0)和查找过程中单词的比较次数。查找前,先将所有字典中单词读入至一个单词表(数组)中,然后按要求进行查找。字典中单词总数不超过3500,单词中的字符都是英文小写字母,并已按字典序排好序。字典中的单词和待查找单词的字符个数不超过20。
按下面给定的hash函数为字典中单词构造一个hash表,hash冲突时按字典序依次存放单词。hash查找遇到冲突时,采用链地址法处理,在冲突链表中找到或未找到(遇到第一个比待查找的单词大的单词或链表结束)便结束查找。
#define NHASH 3001
#define MULT 37
unsigned int hash(char *str)
{
unsigned int h=0;
char *p;
for(p=str; p!=’\0’; p++)
h = MULT h + *p;
return h % NHASH;
}
提示:hash表可以构建成指针数组,hash冲突的单词形成一有序链表。
输入格式
读入单词字典和待查找单词。首先是单词字典,一行一个单词,-1结束。然后是待查找单词,只包含英文小写字母。
输出格式
将查找结果和单词比较次数输出到标准输出上,两整数之间以一个空格分隔。
说明/提示
输入是单词字典,以-1结束,后面一行是待查找单词,输出是查找结果和查找次数,0表示没有找到,1表示查找次数。
wins在单词字典中不存在,输出结果0,hash查找的单词比较次数为2次(wins的hash位置与字典中physics和suggest相同)。
yes在单词字典中存在,输出结果1,hash查找的单词比较次数为1。
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<cstring>
#include<string.h>
#define NHASH 3001
#define MULT 37
using namespace std;
unsigned int hashhh(char* str) {
unsigned int h = 0;
char* p;
for (p = str; *p != '\0'; p++)
h = MULT * h + *p;
return h % NHASH;
}
typedef struct node
{
char word[20];
struct node* next;
}hash;
//构建哈希表
void build(hash* dictionary[])
{
for (int i = 0; i < NHASH; i++)
{
hash* hhh = (hash*)malloc(sizeof(hash));
hhh->next = NULL;
dictionary[i] = hhh;
}
while (1) {
char str[20]; cin >> str;
if (strcmp(str,"-1")==0) break;
hash* p, * q;
p = dictionary[hashhh(str)];
while (p->next != NULL) p = p->next;
if (p->next == NULL) {
q = (hash*)malloc(sizeof(hash));
strcpy(q->word, str);
p->next = q;
q->next = NULL;
}
}
}
//查找
int search(hash* dictionary[])
{
char temp[20]; cin >> temp; int time = 0;
hash* search = dictionary[hashhh(temp)]->next;
if (search == NULL) {
cout << "0 1";
return 0;
}
while (search) {
time++;
if (strcmp(search->word, temp) == 0) {
cout << "1 " << time;
return 1;
}
else if (search->next == NULL) {
cout << "0 " << time;
return 0;
}
search = search->next;
}
}
int main()
{
hash* dictionary[NHASH];
build(dictionary);
search(dictionary);
}