poj 2503(字符串hash)
来源:互联网 发布:生久网络 编辑:程序博客网 时间:2024/04/28 03:29
解题思路(copy别人的):通过ELFhash函数来做。冲突处理则是用到链表的方法。
#include <iostream>#include <fstream>#include <string.h>#define N 100001#define strSize 15using namespace std;struct Hash{ bool used; char fn[strSize],en[strSize]; Hash* next; //用于冲突时构造链表 Hash(){used=false; next=NULL;} Hash(char *f,char *e) { strcpy(fn,f); strcpy(en,e); used=false; next=NULL; } }h[N];int ELFhash(char *key){ unsigned long h=0; unsigned long x=0; while(*key) { h=(h<<4)+(*key++); //h左移4位,当前字符ASCII存入h的低四位 if( (x=h & 0xF0000000L)!=0) { //如果最高位不为0,则说明字符多余7个,如果不处理,再加第九个字符时,第一个字符会被移出 //因此要有如下处理 h^=(x>>24); //清空28~31位 h &=~x; } } return h % N;}int main(){ freopen("acm.txt","r",stdin); char str[30],en[strSize],fn[strSize]; Hash *p; int sign=1,key; while(gets(str)) { if(str[0]=='\0') { sign=0; continue; } if(sign) //输入字典 { sscanf(str,"%s %s",&en,&fn); key=ELFhash(fn); //获取hash值 if(!h[key].used) //对应到hash表中 { h[key].used=true; strcpy(h[key].en,en); strcpy(h[key].fn,fn); } else //处理冲突 { p=&h[key]; while(p->next != NULL) p=p->next; p->next=new Hash(fn,en); } } else //输入外文 { key=ELFhash(str); if(!h[key].used) printf("eh\n"); else { p=&h[key]; while(p!=NULL) { if(!strcmp(str,p->fn)) { printf("%s\n",p->en); break; } else { p=p->next; } } if(p==NULL) printf("eh\n"); //不匹配的情况,不能少 } } } return 0;}
0 0
- poj 2503(字符串hash)
- poj 1200(字符串hash)
- POJ 2418(字符串HASH)
- POJ 1200 Crazy Search (字符串hash)
- POJ 1200 字符串hash
- poj 1200字符串hash
- poj 1200 (字符串hash)
- poj 3349 字符串hash
- POJ 3007(字符串hash)
- POJ 1200字符串hash
- POJ 2503 BABELFISH (HASH)
- POJ 2946 字符串hash + BFS
- Poj 2774 二分+字符串hash
- POJ-1200(N进制字符串hash)
- POJ题目1200 Crazy Search(字符串hash)
- POJ-3007 Organize Your Train part II (字符串hash)
- POJ 3007-Organize Your Train part II(hash-字符串)
- POJ 2503 Babelfish(hash + map)
- centos下mysq5.1.73安装并配置主从
- std::forward 完美转发
- linux用户和用户组管理
- 织梦动态无法更新bug
- python基础教程共60课-第26课操作list
- poj 2503(字符串hash)
- shell之test
- 启动工程时总会报一些奇葩的问题,Failed to read candidate component class,Caused by: java.lang.IncompatibleClassChang
- 01背包
- 前端开发者都应知道的 jQuery 小技巧
- 黑马程序员————————多线程(死锁的原理)
- 使用js生成n到m间的随机数字
- 开机广播BOOT_COMPLETED
- Android Studio 1.5+ 配置Android Annotations框架