poj 2503 Babelfish 【hash】

26 篇文章 0 订阅
6 篇文章 0 订阅

HASH:141MS

#include<cstdio>
#include<cstring>
#define th(a) this->a=a;
const int maxn=100000+10;
const int prime=25013;
struct Hash
{
    char *s1,*s2;
    Hash *nxt;
}hashpool[maxn];
Hash *head[prime+1],*ph;
char buf[maxn<<1][11];
inline unsigned int hash(char *url,int mod=prime)
{
    unsigned int h=0,g;
    while(*url)
    {
        h=(h<<4)+*url++;
        if(g=h&0xf0000000) h^=g>>24;
        h&=~g;
    }
    return h%mod;
}
int main()
{
    int i,j,poolcnt=0,bufcnt=0,p;
    char str[50],*s1,*s2;
    while(gets(str)&&str[0])
    {
        s1=buf[bufcnt],s2=buf[bufcnt+1];
        for(i=0;str[i]!=' ';i++)
            s1[i]=str[i];
        s1[i]=0;
        while(str[++i]==' ');
        for(j=0;str[i];i++)
            s2[j++]=str[i];
        s2[j]=0;
        p=hash(s2);ph=hashpool+poolcnt++;
        ph->s1=s1;ph->s2=s2;ph->nxt=head[p];
        head[p]=ph;
        bufcnt+=2;
    }
    while(gets(str)!=NULL)
    {
        p=hash(str);ph=head[p];
        while(ph!=NULL)
        {
            if(strcmp(str,ph->s2)==0)
            {
                puts(ph->s1);break;
            }
            ph=ph->nxt;
        }
        if(ph==NULL) puts("eh");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值