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;
    }
  • 相关阅读:
    fastlane 自动化打包不同的target,以及手动传版本号参数
    xcode 添加Account报错 This action could not be completed,Try again
    iOS 如何在模拟器中安装APP
    git 基础总结
    cnpm run build 报错 Node Sass could not find a binding for your current environment: OS X 64-bit with Node.js 9.x
    iOS 静态库sdk项目依赖到工程项目测试
    Windows下程序的自删除
    80x86指令码查询表(转载)
    C语言实现Win32第一个窗口
    Visual Studio的Unicode和ASCII
  • 原文地址:https://www.cnblogs.com/littlepage/p/12822359.html
Copyright © 2011-2022 走看看