您的位置:首页 > 其它

POJ_2503(基本Hash)

2010-07-30 23:59 525 查看
#include <cstdlib>
#include <iostream>
#include <cstdio>
using namespace std;
const int MAX = 100000;
const int HASH_SIZE = 100003;
const int STR_LEN = 11;
typedef struct _NODE
{
int hashIndex;
struct _NODE *next;
}NODE, *PTRNODE;
PTRNODE link[HASH_SIZE] = {NULL};
char dic[MAX][STR_LEN];
char words[MAX][STR_LEN];
int BKDRHash(char *str)
{
int size = strlen(str);
int seed = 131;
unsigned long hash = 0;

for(int i=0; i<size; i++)
hash = (hash * seed) + (*str++);

return hash % HASH_SIZE;
}
int main(int argc, char *argv[])
{
//freopen("input.txt", "rt", stdin);
//freopen("output.txt", "wt", stdout);

char str[50];
PTRNODE ptr = NULL;
int index = 0;

gets(str);

while (strcmp(str,"")!=0)
{
int i, hashValue;
for (i=0;str[i]!=' ';i++)
dic[index][i]=str[i];
dic[index][i++]='/0';
strcpy(words[index],str+i);

hashValue = BKDRHash(words[index]);
ptr = (PTRNODE)malloc(sizeof(NODE));
ptr->hashIndex = index++;
ptr->next = link[hashValue];
link[hashValue] = ptr;
gets(str);
}

while (gets(str)!=NULL)
{
int hashValue = BKDRHash(str);
int flag = 0;

ptr = link[hashValue];

while(ptr != NULL)
{
if(strcmp(str, words[ptr->hashIndex]) == 0)
{
printf("%s/n", dic[ptr->hashIndex]);
flag = 1;
break;
}
ptr = ptr->next;
}

if(!flag)
printf("eh/n");
}

return EXIT_SUCCESS;
}
 

 

链地址法解决冲突...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  null struct include