poj2503 Babelfish (hash)
2009-05-14 09:25
375 查看
这是用ELFHash做的版本:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #define MAX 100003
5 struct ENTRY{
6 char foreign[11];
7 char english[11];
8 }entry[MAX];
9
struct LINK{
int hash;
struct LINK *next;
}*link[MAX];
unsigned int ELFHash(char *str)
{
unsigned int hash = 0;
unsigned int x = 0;
while (*str)
{
hash = (hash << 4) + (*str++);
if ((x = hash & 0xF0000000L) != 0)
{
hash ^= (x >> 24);
hash &= ~x;
}
}
return (hash % MAX);
}
int main()
{
int i, key, count=0, flag;
char msg[50];
struct LINK *p;
//freopen("input.txt", "r", stdin);
while (gets(msg) && strcmp(msg, "")!=0){
//¶ÁÈëÊý¾Ý
for (i=0;msg[i]!=' ';i++){
entry[count].english[i]=msg[i];
}
entry[count].english[i]='\0';
strcpy(entry[count].foreign, msg+i+1);
//hash´¦Àí
key=ELFHash(entry[count].foreign);
p=(struct LINK *)malloc(sizeof(struct LINK));
p->hash=count;
p->next=link[key];
link[key]=p;
count++;
}
//²éÕÒ
while (gets(msg)){
flag=0;
key=ELFHash(msg);
p=link[key];
while (p){
if (strcmp(msg, entry[p->hash].foreign)==0){
flag=1;
break;
}
p=p->next;
}
if (flag==1){
printf("%s\n", entry[p->hash].english);
}
else{
printf("eh\n");
}
}
//freopen("CON", "r", stdin);
//system("PAUSE");
return 0;
}
Memory:5872K
Time:297MS
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #define MAX 100003
5 struct ENTRY{
6 char foreign[11];
7 char english[11];
8 }entry[MAX];
9
struct LINK{
int hash;
struct LINK *next;
}*link[MAX];
unsigned int ELFHash(char *str)
{
unsigned int hash = 0;
unsigned int x = 0;
while (*str)
{
hash = (hash << 4) + (*str++);
if ((x = hash & 0xF0000000L) != 0)
{
hash ^= (x >> 24);
hash &= ~x;
}
}
return (hash % MAX);
}
int main()
{
int i, key, count=0, flag;
char msg[50];
struct LINK *p;
//freopen("input.txt", "r", stdin);
while (gets(msg) && strcmp(msg, "")!=0){
//¶ÁÈëÊý¾Ý
for (i=0;msg[i]!=' ';i++){
entry[count].english[i]=msg[i];
}
entry[count].english[i]='\0';
strcpy(entry[count].foreign, msg+i+1);
//hash´¦Àí
key=ELFHash(entry[count].foreign);
p=(struct LINK *)malloc(sizeof(struct LINK));
p->hash=count;
p->next=link[key];
link[key]=p;
count++;
}
//²éÕÒ
while (gets(msg)){
flag=0;
key=ELFHash(msg);
p=link[key];
while (p){
if (strcmp(msg, entry[p->hash].foreign)==0){
flag=1;
break;
}
p=p->next;
}
if (flag==1){
printf("%s\n", entry[p->hash].english);
}
else{
printf("eh\n");
}
}
//freopen("CON", "r", stdin);
//system("PAUSE");
return 0;
}
Memory:5872K
Time:297MS
相关文章推荐
- POJ 2503 Babelfish(HASH)
- POJ 2503 Babelfish(Hash)
- POJ 2503 Babelfish(map,字典树,快排+二分,hash)
- POJ 2503 Babelfish(map,字典树,快排+二分,hash)
- POJ 2503 BABELFISH (HASH)
- Poj 2503 Babelfish (Trie / STL / BKDR Hash)
- poj 2503 Babelfish 【hash】
- POJ 2503 Babelfish (Hash)
- POJ 2503 Babelfish(hash + map)
- Poj 2503 / OpenJudge 2503 Babelfish
- ACM: hash题 poj 2503
- POJ 2503 Babelfish
- poj_2503_Babelfish(字典树&&map)
- POJ 2503 Babelfish
- poj 2503 Babelfish
- POJ 2503 Babelfish [map & string]
- poj 2503 Babelfish
- POJ 2503 Babelfish
- poj 2503 Babelfish
- POJ 2503, Babelfish