#include <iostream> #include<cstdio> #include<cstring> using namespace std; const int M = 149993; typedef struct { char e[11]; char f[11]; int next; }Entry; Entry entry[M]; int i = 1; int hashIndex[M];//哈希表 int ELFHash(char *key) { unsigned long h=0; while(*key) { h=(h<<4)+(*key++); unsigned long g=h&0Xf0000000L; if(g) h^=g>>24; h&=~g; } return h%M; } void find(char f[]) { int hash = ELFHash(f); for(int k = hashIndex[hash]; k; k = entry[k].next) { if(strcmp(f, entry[k].f) == 0) { printf("%s\n",entry[k].e); return; } } printf("eh\n"); } int main() { char str[22]; while(gets(str)) { if(str[0] == ‘\0‘) break; sscanf(str,"%s %s",entry[i].e,entry[i].f);//按格式给str赋值 int hash = ELFHash(entry[i].f); entry[i].next = hashIndex[hash]; hashIndex[hash] = i; i++; } while(gets(str)) find(str); return 0; }
时间: 2024-11-07 01:58:17