pku 2872Spelling Be

47MS,用BKDRHash+链表解决冲突

#include <stdio.h>
#include <string.h>

 

#define MAX1 50003
#define MAX2 200

 

typedef struct node
{
    int ind;
    node *next;
} node;
node newnode[MAX1];
int newn;

typedef struct hashnode
{
    node *first;
} hashnode;
hashnode hashtable[MAX1];

inline int BKDRHash(char *str)
{
    unsigned int seed = 13131; // 31 131 1313 13131 131313 etc..
    unsigned int hash = 0;
    while (*str)
    {
        hash = hash * seed + (*str++);
    }
    return hash%MAX1;
}

char dic[MAX1][MAX2],ans[MAX1][MAX2];

int main()
{
    int index,n;
    scanf("%d",&n);
    node *ptr=NULL;
    int k;
    for(index=0; index<n; index++)
    {
        scanf("%s",dic[index]);
        k=BKDRHash(dic[index]);
        for(ptr=hashtable[k].first; ptr && strcmp(dic[ptr->ind],dic[index]) !=0 ; ptr=ptr->next) ;
        if(!ptr)
        {
            ptr=&newnode[newn++];
            ptr->ind=index;
            ptr->next=hashtable[k].first;
            hashtable[k].first=ptr;
        }
    }
    char email[MAX2];
    bool flag;
    scanf("%d",&n);
    for(index=1; index<=n; index++)
    {
        flag=true;
        scanf("%s",email);
        while(strcmp(email,"-1")!=0)
        {
            k=BKDRHash(email);
            for(ptr=hashtable[k].first; ptr && strcmp(dic[ptr->ind],email)!=0 ; ptr=ptr->next) ;
            if(!ptr)
            {
                if(flag) printf("Email %d is not spelled correctly.\n",index);
                printf("%s\n",email);
                flag=false;
            }
            scanf("%s",email);
        }
        if(flag)
        {
            printf("Email %d is spelled correctly.\n",index);
        }
    }
    printf("End of Output\n");
    return 0;
}

 

posted @ 2010-08-18 14:31  菜到不得鸟  阅读(237)  评论(0)    收藏  举报