POJ 2503 Babelfish
ELFHash开地址 内存:6060kB 时间:70ms
1 #include <stdio.h> 2 #include <iostream> 3 #include <string.h> 4 #include <cstring> 5 #define PRI 93961 6 using namespace std; 7 8 struct DICT{ 9 char keyw[11]; //外语 10 char oriw[11]; //母语 11 DICT *next; 12 DICT(){ next = NULL; keyw[0] = '\0'; } 13 }dict[PRI]; 14 15 int calcuk(char *s){ 16 int len = strlen(s); 17 unsigned long h = 0; 18 while (*s){ 19 h = (h << 4) + *s++; 20 unsigned long g = h & 0xF0000000L; 21 if (g) h ^= g >> 24; 22 h &= ~g; 23 } 24 return h%PRI; 25 } 26 27 char st[25]; 28 int main(){ 29 int key; 30 DICT tmp; 31 DICT *p; 32 while (gets(st)){ 33 if (sscanf(st,"%s %s", tmp.oriw, tmp.keyw) != 2) break; 34 key = calcuk(tmp.keyw); 35 if (dict[key].keyw[0] == '\0') dict[key] = tmp; //该key位为空则直接插入 36 else //否则找到链尾,插入 37 { 38 DICT *node = new DICT; 39 memcpy(node->keyw, tmp.keyw, 11); 40 memcpy(node->oriw, tmp.oriw, 11); 41 42 if (!dict[key].next) dict[key].next = node; 43 else{ 44 p = dict[key].next; 45 while (p->next) p = p->next; 46 p->next = node; 47 } 48 } 49 } 50 while (scanf("%s", st) != EOF){ 51 key = calcuk(st); 52 if (dict[key].keyw[0] == '\0') printf("eh\n"); 53 else if (!strcmp(dict[key].keyw, st)) printf("%s\n", dict[key].oriw); 54 else{ 55 p = dict[key].next; 56 if (!p) printf("eh\n"); 57 else{ 58 while(1){ 59 if (!strcmp(p->keyw, st)){ 60 printf("%s\n", p->oriw); 61 break; 62 } 63 p = p->next; 64 if (!p){ 65 printf("eh\n"); 66 break; 67 } 68 } 69 } 70 } 71 } 72 return 0; 73 }
浙公网安备 33010602011771号