zoukankan      html  css  js  c++  java
  • 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]);
                    }
                }
            }
        }
    }
  • 相关阅读:
    复习题之后缀表达式
    专项训练之枚举
    专项训练之二分
    深夜毒物
    笑面的学校日常(14)最近一次更新2017 04 06
    了不起的竞赛生(7)(最近一次更新2017 04 06)
    水题日常——动态规划 洛谷
    Bzoj 1926: [Sdoi2010]粟粟的书架(二分答案+乱搞+主席树)
    Cogs 1708. 斐波那契平方和(矩阵乘法)
    Codevs 1482 路线统计(矩阵乘法)
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11964366.html
Copyright © 2011-2022 走看看