用得是邻接表,没有用VECTOR,用C++提交
前面我先用MAP水过去,当时用MAP时间是800多MS
ELFHASH 219MS
BKDRHash 235MS
SDBMHash 250MS
RSHash 250MS
JSHash 250MS
PJWHash 250MS
DJBHash 204MS
APHash 266MS
这些时间跟数据都有一定的关系,但是几个函数用时都还是比较稳定的,都在200多MS,还要多做题目,多进行比较,才能有更多的经验
一部分的代码,用ELFHASH
#include<stdio.h>
#include<string.h>
#define N 100003
#define Mod 100003
struct node
{
char english[20],foreign[20];
int next;
}word[N];
int first[N];
int m;
int ELFHash(char* key)
{
unsigned long g,h=0;
while(*key)
{
h=(h<<4)+*key++;
g=h & 0xf0000000L;
if(g) h^=g>>24;
h&=~g;
}
return h%Mod;
}
void insert(char english[],char foreign[])
{
strcpy(word[m].english,english);
strcpy(word[m].foreign,foreign);
int h=ELFHash(foreign);
word[m].next=first[h];
first[h]=m++;
}
int find(char foreign[])
{
int h=ELFHash(foreign);
for(int i=first[h];i!=-1;i=word[i].next)
{
if(strcmp(word[i].foreign,foreign)==0) return i;
}
return -1;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
char str[100];
m=0;
memset(first,-1,sizeof(first));
while(gets(str))
{
if(str[0]==0) break;
char a[20],b[20];
sscanf(str,"%s %s",a,b);
insert(a,b);
}
while(scanf("%s",str)!=EOF)
{
int ans=find(str);
if(ans>=0) printf("%s/n",word[ans].english);
else printf("eh/n");
}
return 0;
}