POJ 2503 Babelfish(HASH)
2010-08-06 01:38
351 查看
//字典HASH简单应用,这题也可用Trie做 //学会了SSCANF的使用,难点在于处理数据的读入 #include<iostream> using namespace std; const int MAXHASH = 100003; int head[MAXHASH],next[MAXHASH]; int m; struct Dic { char english[11]; char foreign[11]; }dic[MAXHASH]; int hash(char *str)//ELFHash函数,黑书刘汝佳推荐使用…… { int h = 0; while(*str) { h = (h << 4) + *str++; int g = h & 0xf0000000L; if(g) h ^= g >> 24; h &= ~g; } return h % MAXHASH; } void insert(char english[],char foreign[]) { strcpy(dic[m].english,english); strcpy(dic[m].foreign,foreign); int h = hash(foreign); next[m] = head[h]; head[h] = m++; } int Find(char foreign[]) { int h = hash(foreign); for(int e = head[h];e != -1;e = next[e]) { if(strcmp(dic[e].foreign,foreign) == 0) return e; } return -1; } int main() { char buff[30]; char english[11],foreign[11],str[11]; m = 0; memset(head,-1,sizeof(head)); while(gets(buff) && buff[0] != 0)//用get读整行,buff[0] == 0表示这行是换行 { sscanf(buff,"%s %s",english,foreign); insert(english,foreign); } while(scanf("%s",str) != EOF) { int ans = Find(str); if(ans >= 0) printf("%s/n",dic[ans].english); else printf("eh/n"); } return 0; }
相关文章推荐
- POJ 2503 Babelfish(map,字典树,快排+二分,hash)
- poj2503 Babelfish (hash)
- POJ 2503 Babelfish(map,字典树,快排+二分,hash)
- POJ 2503 Babelfish(hash + map)
- POJ 2503 Babelfish (Hash)
- POJ 2503 BABELFISH (HASH)
- Poj 2503 Babelfish (Trie / STL / BKDR Hash)
- POJ 2503 Babelfish(Hash)
- poj 2503 Babelfish 【hash】
- POJ_2503_Babelfish【字典树】
- Poj 2503 / OpenJudge 2503 Babelfish
- POJ 2503 Babelfish
- POJ 2503 Babelfish
- poj 2503 Babelfish trie树!!!!指针版和数组版 基础
- Poj 2503 Babelfish(Map操作)
- poj 2503 Babelfish
- POJ2503——Babelfish
- POJ2503——Babelfish
- POJ 2503 Babelfish
- poj2503--Babelfish(字典树一水)