字符串hash模板

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)