zoj 1109

    技术2022-05-11  118

    /*1109.c*/#include <string.h>#include <stdlib.h>#include <stdio.h>struct nlist{    struct nlist *next;    char *fatword;    char *word;};static struct nlist *hashtab[1001];//we should find the better numberunsigned hash(char *s){    unsigned hashval;    for(hashval=0; *s != '/0'; s++)    hashval = *s+31*hashval;    return hashval01;}struct nlist *lookup(char *s){    struct nlist *np;    for(np=hashtab[hash(s)];np!=NULL;np=np->next)    if(strcmp(s,np->fatword)==0)        return np;    return NULL;}char *strdup(char *s){    char *p;    p=(char *)malloc(strlen(s)+1);    if(p!=NULL)    strcpy(p,s);    return p;}struct nlist *install(char *fatword, char *word){    struct nlist *np;    unsigned hashval;    if((np=lookup(fatword))==NULL){        np=(struct nlist *)malloc(sizeof(*np));        if(np==NULL||(np->fatword=strdup(fatword))==NULL)        return NULL;        hashval = hash(fatword);        np->next = hashtab[hashval];        hashtab[hashval]=np;    }else        free((void *)np->word);    if((np->word = strdup(word))==NULL)        return NULL;    return np;}int main(){    char line[200];    char fatword[100];    char word[100];    char fatsay[100];    struct nlist *np;    while(fgets(line,200,stdin)[0]!='/n'){    sscanf(line,"%s%s/n",word,fatword);    np = install(fatword,word);    }    while(scanf("%s/n",fatsay)!=EOF){    np = lookup(fatsay);    if(np==NULL)        printf("eh/n");    else        printf("%s/n",np->word);    }    return 0;}

    最新回复(0)