• 11-散列4 Hashing


    iven a hash table of size N, we can define a hash function (. 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 (≤), 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

    考察hash的线性探测,和拓扑序列,题目大意:已知插入后的哈希表,求原插入顺序。

    #include <iostream>
    #include <vector>
    #include <map>
    #include <set>
    using namespace std;
    map<int, set<int>> G;
    map<int, int> degree;
    int main() {
        int N, tmp, cnt = 0;
        scanf("%d", &N);
        vector<int> v(N), ans;
        for(int i = 0; i < N; i++) {
            scanf("%d", &v[i]);
            if(v[i] > 0) {
                degree[v[i]] = 0; // 入度设置为0
                cnt++;
            }
        }
        for(int i = 0; i < N; i++) {
            if(v[i] > 0) {
                int tmp = v[i];
                while(tmp % N != i) {
                    G[v[tmp % N]].insert(v[i]);
                    tmp++;
                    degree[v[i]]++;
                }
            }
        }
        int index = 0;
        while(index != cnt) {
            int min;
            for(auto x: degree) {
                if(x.second == 0) {
                    min = x.first;
                    break;
                }
            }
            degree[min]--;
            for(auto x: G[min]) degree[x]--;
            ans.push_back(min);
            index++;
        }
        printf("%d", ans[0]);
        for(int i = 1; i < cnt; i++)
            printf(" %d", ans[i]);
        return 0;
    }
  • 相关阅读:
    剑指offer---二叉搜索树的第k个结点
    剑指offer---把数组排成最小的数
    剑指offer---连续子数组的最大和
    剑指offer---最小的K个数
    Navicat for MySQL(Ubuntu)过期解决方法
    Ubuntu 无法应用原保存的显示器配置
    ubuntu 18.04 install gitlab-ce
    Flask 使用过程
    python版本 3.7.4rc1 (stable) / 3.8.0b1 (pre-release) / 3.9.0a0 (in development)
    Windows10 and MySQL使用
  • 原文地址:https://www.cnblogs.com/littlepage/p/12822359.html
Copyright © 2020-2023  润新知