11-散列4 Hashing - Hard Version (30 分)-------C语言

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

        这题我们自己思考的时候,肯定是这样:找到哪些数字位置是正确的(余数和下标相同),哪些不是正确的,它前面的那些数字都必须在它之前输入。

        举个例子,对于12这个数字,12%11=1,但是1那儿是1,而按照线性探测法,12应该去下标为2的地方,但那儿有一个13,最后12去了下标为3的地方,1和13必须在12前面输入。其他的类似。

        这题的难点在于,怎么用计算机来实现上述思路,其实我们换个思路想想,12本来的下标应该是余数1,现在是3,差值为2,因为前面有2个数先输入,这其实和入度的思路是一样的,只有前面的工序完成了才能进行后面的。也就是说余数和实际下标的差值就是这个数的入度,只有入度为0了,我们才能输出它,也就是将其放进了输出队列。最开始进入输出队列的自然是入度为0的数据,也就是余数和下标相同的那些数据,例如33、1、13等等。

        另一个问题是,怎么让每次输出的是输出队列中最小的那个,第一个想法肯定就是最小堆,输出队列做成最小堆,每次弹出堆顶点就可以了。最小堆的构建可以用PercDown函数,在拓扑排序那儿写过,也可以用一个优先队列容器,重载一下优先级运算符的比较,让小的优先级更高。

//11-散列4 Hashing - Hard Version (30 分)
#include<stdio.h>
#include<queue>
#include<vector>
#include<string.h>
using namespace std;
int a[1005], degree[1005];
vector<int>v[1005];
struct cmp{
    bool operator()(int i, int j){
        return a[i]>a[j];
    }

};
priority_queue <int, vector<int>, cmp> qu;

int main()
{
    int N, i, k, j;
    memset(degree, 0, sizeof degree);
    scanf("%d", &N);
    for(i=0;i<N;i++){
        scanf("%d", &a[i]);
        if(a[i]>=0){
            /*余数*/ k = a[i]%N;
            /*入度*/ degree[i] = (i+N-k)%N;
            if(k==i) qu.push(i);
            else{
                for(j=0;j<=degree[i];j++){
                    v[(k+j)%N].push_back(i);
                }
            }
        }
    }
    int flag = 0;
    while(!qu.empty()){
        int u = qu.top();
        qu.pop();
        if(flag) printf(" ");
        flag = 1;
        printf("%d", a[u]);
        for(i=0;i<v[u].size();i++){
            degree[v[u][i]]--;
            if(degree[v[u][i]]==0) qu.push(v[u][i]);
        }
    }
    return 0;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值