PTA—11-散列4 Hashing - Hard Version (30分)

来源:互联网 发布:科技狗一元云购源码 编辑:程序博客网 时间:2024/05/01 03:23

Given a hash table of size NN, 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 NN (\le 10001000), which is the size of the hash table. The next line contains NN 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:

1133 1 13 12 34 38 27 22 32 -1 21

Sample Output:

1 13 12 21 33 34 38 27 22 32

————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————这道题目呀!参考了下流云碎雨在新浪博客上的那篇文章。。。。原来打算自己写下堆和队列,看到用STL容器这么方便,就决定用了!感觉还是非常不错的用STL写算法。正如陈越老师说的,这道题出的非常巧妙,结合了散列和拓扑排序的知识,最后还用优先队列来实现。

说下这道题目的注意点:

1.建立邻接表的话我是采用了VECTOR类来实现,大家可以在网上找找资料有很多我就不叙述了。

2.hashtable的Key和Key所在的位置要对应起来,我是采用了struct的结构来储存


// PTA——Hashing - Hard Version(拓扑排序的经典!).cpp : Defines the entry point for the console application.//#include <vector>#include <queue>#include<iostream>#include <functional>using namespace std;struct HashNode{int Hash;int Key;friend bool operator< (HashNode n1, HashNode n2){        return n1.Key > n2.Key;     }};int* Indegree;                              //用来储存各个key对应的入度HashNode* pHashNode;void ToplogicSort(vector<vector<int>> rating,int num){vector<int>TopNum;int i;                         priority_queue<HashNode>Q;for(i=0;i<num;i++)if(!Indegree[i])Q.push(pHashNode[i]);while(!Q.empty()){HashNode h=Q.top();int V=h.Hash;TopNum.push_back(h.Key);Q.pop();for(i=0;i<rating[V].size();i++)if(--Indegree[rating[V][i]]==0)Q.push(pHashNode[rating[V][i]]);}printf("%d",TopNum[0]);for(i=1;i<TopNum.size();i++)printf(" %d",TopNum[i]);}int main(){int i,j,num,currentpos,hashpos; scanf("%d",&num);vector<vector<int>> rating(num);            //用vector来模拟邻接表pHashNode=new HashNode[num];Indegree=new int[num];for(i=0;i<num;i++){scanf("%d",&pHashNode[i].Key);pHashNode[i].Hash=i;if(pHashNode[i].Key==-1){Indegree[i]=-1;continue;}currentpos=i;hashpos=pHashNode[i].Key%num;                   //根据hash函数计算出来的位置Indegree[i]=(currentpos-hashpos+num)%num;//计算发生冲突的次数,即入度数for(j=0;j<Indegree[i];j++)rating[(hashpos+j+num)%num].push_back(i); //建立邻接表,把从通过函数计算出的位置到当前位置前的点的vector里都加上当前点的下标}ToplogicSort(rating,num);return 0;}


0 0
原创粉丝点击