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; }
链地址法解决冲突...
相关文章推荐
- POJ 2503 Babelfish(map,字典树,快排+二分,hash)
- POJ 2503 BABELFISH (HASH)
- poj 2503(字符串hash)
- POJ 2503 Babelfish(hash + map)
- POJ 2503 Babelfish(Hash)
- POJ 2503 二分 还有一些基本字符串的处理
- poj 2503 【hash】【map】
- poj 2503 Babelfish 【hash】
- POJ 2503 Babelfish (Hash)
- POJ 2503 Babelfish hash / qsort+ bsearch
- POJ 2503 Hash
- 【留坑】 POJ2503 注意输入输出&&几种数据结构的复习、比较(线性表建立的字典 || 树建立的字典_即Trie || hash || map)
- POJ 2503 Babelfish(HASH)
- Poj 2503 Babelfish (Trie / STL / BKDR Hash)
- hash Babelfish poj 2503
- poj2503 Babelfish (hash)
- 【POJ 1635】树基本操作(Tree 重心 + Tree hash + ULL 的用法)
- poj 2503 Babelfish(hash or 字典树)
- ACM: hash题 poj 2503
- poj 2503 hash