码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构 11-散列4 Hashing - Hard Version (30 分)

时间:2021-06-02 12:19:23      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:nta   contain   har   之间   数据结构   sample   最小值   ==   tin   

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

 

 

给定一个size为n的hashtable ,-1表示该位置没有数据

逆推输入队列

 

 

维护一个动态队列, 每次从中取出一个最小值输出, 队列中存放的是可以放入hash列表的值,

其中包括 1.自身哈希值等于列表最终位置   2.哈希值不等于列表最终位置,但是列表最终位置和哈希值之间的元素都被占满了的元素

每趟循环取出可以输出的最小值之后, 重新遍历待处理的剩余列表 , 继续取出可输出元素放入输出队列

 

#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
int main(){
    int n,temp,index;
    cin >> n;
    vector<int> outputList;//待输出列表
    vector<int> visited(n,0);
    vector<int> restList;//不能输出的待处理列表
    unordered_map<int, int> indexmap;//元素在hash表中的最终位置
    for(int i=0;i<n;i++){
        cin >> temp;
        if(temp!=-1){
            index=temp%n;
            indexmap[temp]=i;
            if(index==i){
                outputList.push_back(temp);
            }else{
                restList.push_back(temp);
            }
        }
    }
    bool flag=false;
    while(!outputList.empty()){
        sort(outputList.begin(), outputList.end());
        if(flag){
            cout <<" ";
        }else{
            flag=true;
        }
        cout << outputList.front();
        index=outputList.front()%n;
        outputList.erase(outputList.begin());
        
        for(int i=index;i<visited.size();i=(i+1)%n){
            if(visited[i]==0){
                visited[i]=1;
                break;
            }
        }
        for(int j=0;j<restList.size();j++){
            if(restList[j]!=-1){
                index=restList[j]%n;
                bool flag{true};
                int realIndex=indexmap[restList[j]];
                for(int k=index;k!=realIndex;k=(k+1)%n){
                    if(visited[k]==0){
                        flag=false;
                        break;
                    }
                }
                if(flag){//可以输出当前元素,加入待输出列表,将元素置为空
                    outputList.push_back(restList[j]);
                    restList[j]=-1;
                }
            }
        }
    }
    return 0;
}

 

数据结构 11-散列4 Hashing - Hard Version (30 分)

标签:nta   contain   har   之间   数据结构   sample   最小值   ==   tin   

原文地址:https://www.cnblogs.com/ichiha/p/14815466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!