zoukankan      html  css  js  c++  java
  • 7-18 Hashing

    Given 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

    题目要求是给出了hashing的结果,而且是用的linear probing线性探测解决冲突,要求给出初始序列。负数直接跳过,序列中的数都是非负数。所以遍历给出序列,如果说当前位置的s[i] % n等于i那么就是说不存在冲突,如果不相等就是存在冲突的,经过线性探测才存在当前位置,所以从s[i] % n位置到i之前的数都是比是s[i]先进行操作的,如此我们可以找到序列中数的顺序,进行拓扑排序,如果存在多种可能要求输出最小的,所以用优先队列。
    代码:
    #include <cstdio>
    #include <vector>
    #include <queue>
    #include <algorithm>
    using namespace std;
    
    int n,s[1000],l[1000],flag;
    vector<int> v[1000];
    struct cmp {
        bool operator ()(const int &a,const int &b) {
            return s[a] > s[b];
        }
    };
    int main() {
        scanf("%d",&n);
        for(int i = 0;i < n;i ++) {
            scanf("%d",&s[i]);
        }
        priority_queue<int,vector<int>,cmp> q;
        for(int i = 0;i < n;i ++) {
            if(s[i] < 0) continue;
            if(s[i] % n != i) {
                int d = s[i] % n > i ? i + n : i;
                for(int j = s[i] % n;j < d;j ++) {
                    if(s[j % n] < 0) continue;
                    v[j % n].push_back(i);
                    l[i] ++;
                }
            }
            else q.push(i);
        }
        while(!q.empty()) {
            int temp = q.top();
            q.pop();
            if(flag) putchar(' ');
            else flag = 1;
            printf("%d",s[temp]);
            for(int i = 0;i < v[temp].size();i ++) {
                int d = v[temp][i];
                l[d] --;
                if(!l[d]) q.push(d);
            }
        }
        return 0;
    }
  • 相关阅读:
    js跳出循环
    JavaScript prototype属性
    【DP专题】——洛谷P2279:消防局的设立
    转:android中dialog工具类的实现(多种dialog的创建)
    转:setContentView的时候,到底发生了什么
    转:Handler一定要在主线程实例化吗?new Handler()和new Handler(Looper.getMainLooper())的区别
    转:Android Studio中的Gradle是干什么的
    转:Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB
    Android 学习
    com.baidu.navisdk.adapter找不到 在百度定位SDK的基础之上导入导航的SDK(针对新版本的坑!)
  • 原文地址:https://www.cnblogs.com/8023spz/p/12269735.html
Copyright © 2011-2022 走看看