POJ2503 哈希(字符串编码)

Babelfish
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 37822 Accepted: 16111
Description

You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.
Input

Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.
Output

Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as “eh”.
Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay
Sample Output

cat
eh
loops

题意:
给出多个单词组成的字典,有几个查询,问每个查询是否能找到对应的单词。
题解:
这题用map也能水过,不过时间耗时太高。正确的做法是hash。把字符串编码为一个键值放到哈希表里,然后查询。常用的字符串编码都试了一遍,对应耗时标在编码方式上面。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <map>
#include <cstdlib>
#include <cstring>
#include <string>
#define f(i,a,b) for(int i = a;i<=b;i++)
#define fi(i,a,b) for(int i = a;i>=b;i--)
#define M 100003
#define HASH APhash

using namespace std;

struct node{
    int hash;
    struct node* next;
}*link[M] = {NULL};

char word[100000][11],dialect[100000][11],qu[11];

unsigned int APhash(char* str){
    unsigned int hash = 0;
    int i;
    for(i = 0;*str;i++)
        if((i&1)==0)
            hash^=((hash<<7)^(*str++)^(hash>>3));
        else
            hash^=(~((hash<<11)^(*str)^(hash>>5)));
    return (hash&0x7FFFFFFF)%M;
}

//  TLE
unsigned int DJBhash(char* str){
    unsigned int hash = 5381;
    while(*str){
        hash += (hash<<5) + (*str);
    }
    return (hash&0x7FFFFFFF)%M;
}

//  688ms
unsigned int PJWhash(char* str){
    unsigned int BitsInUnignedInt = (unsigned int)(sizeof(unsigned int)*8);
    unsigned int ThreeQuarters = (unsigned int)((BitsInUnignedInt*3)/4);
    unsigned int OneEighth = (unsigned int)(BitsInUnignedInt/8);
    unsigned int HighBits = (unsigned int)(0xffffffff)<<(BitsInUnignedInt - OneEighth);
    unsigned int hash = 0;
    unsigned int test = 0;
    while(*str){
        hash = (hash<<OneEighth)+(*str++);
        if((test = hash&HighBits)!=0)
            hash = ((hash^=(test>>ThreeQuarters))&(~HighBits));
    }
    return (hash&0x7FFFFFFF)%M;
}


//  688ms
unsigned int JShash(char* str){
    unsigned int hash = 1315423911;
    while(*str){
        hash^=((hash<<5)+(*str++)+(hash>>2));
    }
    return (hash&0x7FFFFFFF)%M;
}


//  704ms
unsigned int RShash(char* str){
    unsigned int b = 378551;
    unsigned int a = 63689;
    unsigned int hash = 0;
    while(*str){
        hash = hash*a + (*str++);
        a*=b;
    }
    return (hash&0x7FFFFFFF)%M;
}

//  719ms
unsigned int SDBMhash(char* str){
    unsigned int hash = 0;
    while(*str)
        hash = (*str++) + (hash<<6) + (hash<<16) - hash;
    return (hash&0x7FFFFFFF)%M;
}

//  688ms
unsigned int BKDRhash(char* str){
    unsigned int seed = 1313131; //31 131 1313 13131 131313
    unsigned int hash = 0;
    while(*str)
        hash = hash*seed + (*str++);
    return (hash&0x7FFFFFFF)%M;
}

//   641ms 常用 linux文件
int ELFhash(char* key){
    unsigned long h = 0,g;
    while(*key){
        h = (h<<4) + *key++;
        g = h&0xf0000000L;
        if(g)
            h^=g>>24;
        h&=~g;
    }
    return h%M;
}

int main()
{
//    freopen("data.in","r",stdin);
    int n = 0;
    struct node* p;
    while(1){
        char c;
        c = getchar();
        if(c == '\n') break;
        else word[n][0] = c;
        scanf("%s %s",word[n]+1,dialect[n]);
        getchar();
        int e = HASH(dialect[n]);
        p = (struct node*) malloc(sizeof(struct node));
        p->hash = n;
        p->next = link[e];
        link[e] = p;
        n++;
    }
    while(1){
        char c;
        c = getchar();
        if(c == -1) break;
        else qu[0] = c;
        scanf("%s",qu+1);
        getchar();
        int e = HASH(qu);
        p = link[e];
        while(p!=NULL){
            if(strcmp(qu,dialect[p->hash])==0)
                break;
            p = p -> next;
        }
        if(p == NULL)
            printf("eh\n");
        else
            printf("%s\n",word[p->hash]);

    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值