Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

求大神给我看看我的hash,wa无数次了

Posted by taaini8691 at 2014-08-06 16:14:53 on Problem 2503
#include<iostream>
#include<cstdio>
#include<string.h>

using namespace std;

struct hashtable{
char en[11];
char f[11];
hashtable *next;
hashtable()
{
    next=0;
}
};
hashtable *hash[300100];


void insert(int key,char *en,char *f)
{
    hashtable *temp=new hashtable();
    strcpy(temp->en,en);
    strcpy(temp->f,f);
    temp->next=NULL;
    if(!hash[key])
    {
        hash[key]=temp;
    }
    else
    {
        hashtable *p=hash[key];
        while(p)
        {
            p=p->next;
        }
        p=temp;
    }
}

void find(int key,char *f)
{
    if(!hash[key])
    {
        printf("eh\n");
        return;
    }
    else
    {
        hashtable *p=hash[key];
        while(p)
        {
            if(!strcmp(f,p->f))
               {
                   printf("%s\n",p->en);
                   return;
               }
            p=p->next;
        }
    }
    printf("eh\n");
    return;
}

int main()
{
    memset(hash,0,sizeof hash);
    char en[11],foreign[11],str[25];
    while(1)
    {
        gets(str);
        if(!strlen(str))
            break;
        int i=0,j=0;
        bool flag=true;
        int len=strlen(str);
        int key=0;
        while(j<len)
        {
            if(str[j]==' ')
            {
                flag=false;
                en[i]='\0';
                i=0;
                j++;
                continue;
            }
            if(flag)
            {
                en[i++]=str[j];
            }
            else
            {
                foreign[i++]=str[j];
                key*=26;
                key%=300089;
                key+=str[j]-'a'+1;

            }
            j++;
        }
        foreign[i]='\0';
        key%=300089;
        //printf("%d\n",key);
        insert(key,en,foreign);
    }
    char f1[11];
        while(scanf("%s",f1)!=EOF)
        {
           // puts(f1);
            getchar();
            int key1=0;
            int len1=strlen(f1);
            for(int k=0;k<len1;k++)
            {
                key1*=26;
                key1%=300089;
                key1+=f1[k]-'a'+1;
            }
            key1%=300089;
            find(key1,f1);
        }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator