先附上我的代码:
#include"iostream"
#include"cstring"
#include"cstdlib"
#include"cstdio"
#define N 100003
using namespace std;
typedef struct node
{
char english[15];
char foreign[15];
struct node* next;
}point;
point data[N];
int elfhash(char* str)
{
unsigned long hash=0;
unsigned long x=0;
while(*str)
{
hash=(hash<<4)+(*str++);
if((x=hash&0xF0000000L)!=0)
{
hash^=(x>>24);
}
hash&=~x;
}
return hash%N;
}
int main()
{
char ppp[30];
char s1[12];
char s2[12];
memset(s1,0,sizeof(s1));
memset(s2,0,sizeof(s2));
while(gets(ppp) && ppp[0]!='\0')
{
sscanf(ppp,"%s %s",s1,s2);
int key=elfhash(s2);
point* k=new point;
memset(k,0,sizeof(k)); //这里采用头插法
strcpy(k->english,s1);
strcpy(k->foreign,s2);
k->next=data[key].next;
data[key].next=k;
memset(s1,0,sizeof(s1));
memset(s2,0,sizeof(s2));
}
while(gets(ppp))
{
int key=elfhash(ppp);
point* k=data[key].next;
if(k==NULL) printf("eh\n");
else
{
while(k!=NULL)
{
if(strcmp(k->foreign,ppp)==0)
{
//while(1);
printf("%s\n",k->english); //找到匹配的我退出
break;
}
k=k->next;
}
}
memset(ppp,0,sizeof(ppp));
}
return 0;
}
本题我才用了ELFhash的方法,但是一直在WA,所以很是无语,找不出BUG,在这里悬赏,大恩不言谢
http://blog.csdn.net/yeruby/article/details/43154399
http://blog.csdn.net/cnyali_ljf/article/details/51340383
http://blog.csdn.net/lmfqyj/article/details/51367266