• 11-散列4 Hashing


    Given a hash table of size N, we can define a hash function H(x)=x%N. Suppose that the linear probing is used to solve collisions, we can easily obtain the status of the hash table with a given sequence of input numbers.

    However, now you are asked to solve the reversed problem: reconstruct the input sequence from the given status of the hash table. Whenever there are multiple choices, the smallest number is always taken.

    Input Specification:

    Each input file contains one test case. For each test case, the first line contains a positive integer N(1000), which is the size of the hash table. The next line contains N integers, separated by a space. A negative integer represents an empty cell in the hash table. It is guaranteed that all the non-negative integers are distinct in the table.

    Output Specification:

    For each test case, print a line that contains the input sequence, with the numbers separated by a space. Notice that there must be no extra space at the end of each line.

    Sample Input:

    11
    33 1 13 12 34 38 27 22 32 -1 21
    

    Sample Output:

    1 13 12 21 33 34 38 27 22 32

    /*知道散列数组大小,和散列数组值反求输入数组*/
    #include<cstdio>
    #include<cstdlib>
    #include<set>
    using namespace std;
    const int maxn = 1010;
    int G[maxn][maxn];
    
    void BuildGraph(int hash[], int n);
    void TopSort(int hash[], int hashMap[], int n, int num);
    
    int main()
    {
        int n;
        int num = 0;    //纪录hash表中元素个数 
        int hash[maxn], hashMap[maxn];
        scanf("%d", &n);
        
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &hash[i]);
            hashMap[hash[i]] = i;
            if (hash[i] >= 0)
            {
                num++;
            }
        }
        
        BuildGraph(hash, n);
        TopSort(hash, hashMap, n, num);
        return 0;
    }
    
    void BuildGraph(int hash[], int n)
    {
        for (int i = 0; i < n; i++)
        {
            if (hash[i] >= 0)
            {
                int tmp = hash[i] % n;    //对应采用线性检查法解决碰撞的元素 
                if (hash[tmp] != hash[i])
                {
                    for (int j = tmp; j != i; j = (j+1) % n)    //建立有向拓扑图来 
                    {
                        G[j][i] = 1;    //凡是在这个元素之前的位置,都要依赖他们先输入 
                    }
                }
            }
        }
    }
    
    void TopSort(int hash[], int hashMap[], int n, int num)
    {
        int cnt = 0;
        int Indegree[maxn] = {0};
        set<int> s;
        
        for (int v = 0; v < n; v++)
        {
            for(int w = 0; w < n; w++)
            {
                if (G[v][w] != 0)
                {
                    Indegree[w]++;
                }
            }
        }
        
        for (int i = 0; i < n; i++)
        {
            if (Indegree[i] == 0 && hash[i] > 0)
            {
                s.insert(hash[i]);
            }
        }
        
        while (!s.empty())
        {
            int now = hashMap[*s.begin()];
            s.erase(s.begin());
            cnt++;
            printf("%d", hash[now]);
            if (cnt != num)
            {
                printf(" ");
            }
            
            for (int v = 0; v < n; v++)
            {
                if (G[now][v] != 0)
                {
                    if (--Indegree[v] == 0)
                    {
                        s.insert(hash[v]);
                    }
                }
            }
        }
    }
  • 相关阅读:
    在 Windows 10 中创建任何大小的虚拟测试文件的 2 种方法
    最近的github又不稳了。。ip host 大法来
    windows mklink /d /h /j 精讲
    kafka-manager配置和使用
    Java——七种垃圾收集器+JDK11最新ZGC
    聚簇索引和非聚簇索引(通俗易懂 言简意赅)
    【转载】Java中的锁机制 synchronized & 偏向锁 & 轻量级锁 & 重量级锁 & 各自优缺点及场景 & AtomicReference
    都1202年了奉劝那些还在用centos6的gs,赶紧切ubuntu-Centos6 升级 gcc 惨痛教训
    Tamper Chrome – 请求修改扩展,可用于Web安全测试
    线程、线程池三大方法、七大参数、四种策略
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11964366.html
Copyright © 2020-2023  润新知