码迷,mamicode.com
首页 > 其他好文 > 详细

字符串hash模板

时间:2015-03-15 18:24:45      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

int BKDRHash(char *str)
{
    int seed = 131; 
    int hash = 0;
    while (*str) hash = hash * seed + (*str++);
    return (hash & 0x7FFFFFFF) % MAXN;
}

 

struct HashMap
{
    int head[Hash + 10]; int next[maxn + 10]; int val[maxn + 10]; int ans[maxn + 10][31];
    int pos[maxn + 10];
    int size;
    void init()
    {
        size = 0; memset(head, -1, sizeof(head));
    }
    int ask(int a[])
    {
        int t = 0;
        for (int i = 0; i < k; i++) t += a[i];
        int gg = (t%Hash + Hash) % Hash;
        for (int i = head[gg]; i != -1; i = next[i]){
            if (val[i] == t){
                int flag = 0;
                for (int j = 0; j < k; j++) {
                    if (ans[i][j] != a[j]) {
                        flag = 1; break;
                    }
                }
                if (flag == 0) return pos[i];
            }
        }
        return -2;
    }
    void insert(int a[], int p)
    {
        int t = 0;
        for (int i = 0; i < k; i++) t += a[i];
        int gg = (t%Hash + Hash) % Hash;;
        for (int i = head[gg]; i != -1; i = next[i]){
            if (val[i] == t){
                int flag = 0;
                for (int j = 0; j < k; j++){
                    if (ans[i][j] != a[j]){
                        flag = 1; break;
                    }
                }
                if (flag == 0) return;
            }
        }
        val[size] = t; next[size] = head[gg];
        pos[size] = p;
        for (int i = 0; i < k; i++) ans[size][i] = a[i];
        head[gg] = size++;
    }
}m;

 

字符串hash模板

标签:

原文地址:http://www.cnblogs.com/yigexigua/p/4340040.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!