标签:class blog code ext get 2014
有关字符串的hash,用黑书上推荐的传说中的ELFhash函数
输入的话,用sscanf处理比较简洁
#include<iostream> #include<cstring> using namespace std; #define M 9991 struct n1 { char foreigh[11],english[11]; n1 *next; }; n1 hash[M]; int ELFhash(char *key) { long long g,h=0; while(*key) { h=(h<<4)+*key++; g=h&0xf0000000L; if(g) h^=g>>24; h&=~g; } return h%M; } void find(char *s) { n1 *p; for(p=&hash[ELFhash(s)];p->next!=0;p=p->next) if(strcmp(p->foreigh,s)==0) { printf("%s\n",p->english); return; } printf("eh\n"); } void insert(char *e,char *f) { n1 *p; for(p=&hash[ELFhash(f)];p->next!=0;p=p->next); strcpy(p->english,e); strcpy(p->foreigh,f); p->next=new(n1); p->next->next=0; } void datain() { char tmp[22],english[11],foreigh[11]; while(gets(tmp)&&tmp[0]) { sscanf(tmp,"%s %s",english,foreigh); insert(english,foreigh); } } void solve() { char foreigh[11]; memset(hash,0,sizeof(hash)); datain(); while(scanf("%s",foreigh)!=EOF) find(foreigh); } int main() { solve(); }
标签:class blog code ext get 2014
原文地址:http://blog.csdn.net/stl112514/article/details/32098799