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

UNIX下的字符串哈希函数

Posted by moorage at 2011-04-25 01:26:57 on Problem 3274
UNIX下的字符串哈希函数
int ELFhash(char *key) /// 此乃UNIX系统使用的哈希,绝对有价值
{
    unsigned long h = 0;
    while (*key){
        h = (h << 4) + *key++;
        unsigned long g = h & 0xf0000000L; // 1个f,7个0
        if (g) h ^= g >> 24;
        h &= ~g;
    }
    return (h+M) % M; // M is Prime
}

int hashcode(const L&a) /// 把数字的转换为字符串,进行哈希,以后就不用费心思设计哈希了
{
    char str[32*8];
    char *s = (char*)(&a.d[1]);
    int i;
    for (i = 0; i < k-1; i++){
        str[i] = 'a' + s[i];
    }
    str[i] = 0;
    return ELFhash(str);
}
这样就ok了,我的代码跑了400+ms,不算快的。O(∩_∩)O~

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