zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    【机器学习】均方误差(MSE)和均方根误差(RMSE)和平均绝对误差(MAE)
    python自动化测试框架nose
    python单元测试之unittest框架使用总结
    【flask】flask讲解
    Python查询手机号码归属地几种方法
    数据库的备份与恢复
    python中OrderedDict的使用
    python struct 结构体
    【Linux】linux-nfs挂载
    【机器学习】方差为何除以n-1
  • 原文地址:https://www.cnblogs.com/littlepage/p/12822359.html
Copyright © 2011-2022 走看看