zoukankan      html  css  js  c++  java
  • Cuckoo Hashing

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1672

    Cuckoo Hashing

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 311    Accepted Submission(s): 169


    Problem Description
    One of the most fundamental data structure problems is the dictionary problem: given a set D of words you want to be able to quickly determine if any given query string q is present in the dictionary D or not. Hashing is a well-known solution for the problem. The idea is to create a function h : from all strings to the integer range 0, 1, .., n-1, i.e. you describe a fast deterministic program which takes a string as input and outputs an integer between 0 and n?1. Next you allocate an empty hash table T of size n and for each word w in D, you set T[h(w)] = w. Thus, given a query string q, you only need to calculate h(q) and see if T[h(q)] equals q, to determine if q is in the dictionary. 
    Seems simple enough, but aren’t we forgetting something? Of course, what if two words in D map to the same location in the table? This phenomenon, called collision, happens fairly often (remember the Birthday paradox: in a class of 24 pupils there is more than 50% chance that two of them share birthday). On average you will only be able to put roughly sqrt(n)-sized dictionaries into the table without getting collisions, quite poor space usage!
    A stronger variant is Cuckoo Hashing^1. The idea is to use two hash functions h1 and h2. Thus each string maps to two positions in the table. A query string q is now handled as follows: you compute both h1(q) and h2(q), and if T[h1(q)] = q, or T[h2(q)] = q, you conclude that q is in D. The name “Cuckoo Hashing” stems from the process of creating the table. Initially you have an empty table. You iterate over the words d in D, and insert them one by one. If T[h1(d)] is free, you set T[h1(d)] = d. Otherwise if T[h2(d)] is free, you set T[h2(d)] = d. If both are occupied however, just like the cuckoo with other birds’ eggs, you evict the word r in T[h1(d)] and set T[h1(d)] = d. Next you put r back into the table in its alternative place (and if that entry was already occupied you evict that word and move it to its alternative place, and so on). Of course, we may end up in an infinite loop here, in which case we need to rebuild the table with other choices of hash functions. The good news is that this will not happen with great probability even if D contains up to n/2 words!
     
    Input
    On the first line of input is a single positive integer 1 <= t <= 50 specifying the number of test cases to follow. Each test case begins with two positive integers 1 <= m <= n <= 10000 on a line of itself, m telling the number of words in the dictionary and n the size of the hash table in the test case. Next follow m lines of which the i:th describes the i:th word di in the dictionary D by two non-negative integers h1(di) and h2(di) less than n giving the two hash function values of the word di. The two values may be identical.
     
    Output
    For each test case there should be exactly one line of output either containing the string “successful hashing” if it is possible to insert all words in the given order into the table, or the string “rehash necessary” if it is impossible.
     
    Sample Input
    2 3 3 0 1 1 2 2 0 5 6 2 3 3 1 1 2 5 1 2 5
     
    Sample Output
    successful hashing rehash necessary
     
    题目大意:输入m,n代表有m个串 Hash表的大小为n 每个串的Hash值有两个 你的任务是使用类似匈牙利算法的思想,能不能使得Hash表没有冲突
    看代码:
    #include<iostream>
    #include<algorithm>
    #include<stack>
    #include<cstdio>
    #include<map>
    #include<queue>
    #include<cstring>
    using namespace std;
    typedef long long LL;
    const int maxn=1e4+5;
    int N,M;
    int a[maxn][5];
    int link[maxn];
    bool vis[maxn];
    bool dfs(int x)
    {
        for(int i=0;i<2;i++)//两个巢只要占的了一个就够了
        {
            int y=a[x][i];
            if(!vis[y])
            {
                vis[y]=true;
                if(link[y]==-1||dfs(link[y]))//这个巢没有被占领或者这个巢当前占领这个巢的鸟可以转到另一个巢去0
                {
                    link[y]=x;//就把这个巢让给x
                    return true;
                }
            }
        }
        return false;
    }
    int main()
    {
        int T;
        cin>>T;
        while(T--)
        {
            int flag=0;
            memset(link,-1,sizeof(link));
            cin>>N>>M;
            for(int i=0;i<N;i++)
            {
                cin>>a[i][0]>>a[i][1];
            }
            for(int i=0;i<N;i++)//遍历N个串
            {
                memset(vis,false,sizeof(vis));
                if(!dfs(i))
                {
                    flag=1;break;
                }
            }
            if(flag) cout<<"rehash necessary"<<endl;
            else cout<<"successful hashing"<<endl;
        }
    
        return 0;
    }
      
    当初的梦想实现了吗,事到如今只好放弃吗~
  • 相关阅读:
    函数
    拉取代码到本地
    逻辑位运算符 以及 布尔运算符&&、||
    JS中substr与substring的区别
    ? :和!:的用法含义及es6语法...
    JS中attribute和property的区别
    并发、并行的理解
    斑鸠云商小程序记住账号和密码
    js中的foreach用法
    指针与数组
  • 原文地址:https://www.cnblogs.com/caijiaming/p/11163596.html
Copyright © 2011-2022 走看看