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 21Sample Output:
1 13 12 21 33 34 38 27 22 32
http://bbs.csdn.net/topics/320136696
解读在这。。。。。。。。
转自http://blog.sina.com.cn/s/blog_ce1b01420102vizs.html
想不明白就想不明白了,囧。
冲突 = (下标 - Key % N + N) % N
解题思路:
1.把Hash表的序列保存到数组中
2.计算入度,即冲突次数,并建立有向图,把下标当顶点。从Key%N 到 当前下标 - 1的顶点都指向当前下标,因为这些点都影响当前顶点。
3.拓扑排序,把度为0的顶点放入最小堆中,STL可以用优先队列。
4.找出度为0且值最小的顶点,扫描与该点相连的顶点,入度 - 1,如果入度变0,则加入最小堆或者优先队列。
5.取最小堆顶点或者取队列首元素的时候,把该顶点的Key存入数组,当然你也可以直接输出
——————————
下面是AC代码
#include <iostream>
#include <stdlib.h>
#include <queue>
#include <vector>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
vector<int> Ans;
typedef pair<int,int> P;
void TopSort(int N, int *hash, int *indegree, vector<int>* G)
{
priority_queue<pair<int,int>,vector<pair<int,int> >, greater<pair<int,int> > > q;
for(int i=0; i<N; i++)
{
if(indegree[i] == 0)
{
q.push(pair<int,int>(hash[i],i));
}
}
while(!q.empty())
{
P p=q.top();q.pop();
int v=p.second;
Ans.push_back(p.first);
for(int i=0; i<G[v].size(); i++)
{
if(--indegree[G[v][i]] == 0)
{
q.push(P(hash[G[v][i]],G[v][i]));
}
}
}
}
int main(int argc, char** argv) {
int N;
scanf("%d",&N);
int *hash=new int[N];
int *indegree=new int[N];
vector<int> *G=new vector<int>[N];
for(int i=0; i<N; i++)
{
scanf("%d",&hash[i]);
(hash[i]<0)?indegree[i]=-1:indegree[i]=0;
}
for(int i=0; i<N; i++)
{
if(hash[i]<0) continue;
int curpos=i;
int hashpos=hash[i]%N;
indegree[i]=(curpos-hashpos+N)%N;//Èë¶È
// cout<<i<<indegree[i]<<";";
for(int t=0; t<indegree[i]; t++)
{
G[(hashpos+t+N)%N].push_back(i);
}
}
TopSort(N,hash,indegree,G);
cout<<Ans[0];
for(int i=1; i<Ans.size(); i++)
{
cout<<" "<<Ans[i];
}
return 0;
}
——————————————————————
提交,一直编译错误,一定是编译器的问题。
#include <iostream>
#include <stdlib.h>
#include <queue>
#include <vector>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int *hash;
int *indegree;
vector<int> * G;
vector<int> Ans;
void Creat(int size)
{
hash=new int[size];
indegree=new int[size];
G=new vector<int>[size];
// hash=(int *)malloc(size*sizeof(int));
// indegree=(int *)malloc(size*sizeof(int));
// G=(vector<int> *)malloc(size*sizeof(vector<int>));
for(int i=0; i<size; i++)
{
scanf("%d",&hash[i]);
(hash[i]<0)?indegree[i]=-1:indegree[i]=0;
}
}
typedef pair<int,int> P;
void TopSort(int N)
{
priority_queue<pair<int,int>,vector<pair<int,int> >, greater<pair<int,int> > > q;
for(int i=0; i<N; i++)
{
if(indegree[i] == 0)
{
q.push(pair<int,int>(hash[i],i));
}
}
while(!q.empty())
{
P p=q.top();q.pop();
int v=p.second;
Ans.push_back(p.first);
for(int i=0; i<G[v].size(); i++)
{
if(--indegree[G[v][i]] == 0)
{
q.push(P(hash[G[v][i]],G[v][i]));
}
}
}
}
int main(int argc, char** argv) {
int N;
scanf("%d",&N);
Creat(N);
for(int i=0; i<N; i++)
{
if(hash[i]<0) continue;
int curpos=i;
int hashpos=hash[i]%N;
indegree[i]=(curpos-hashpos+N)%N;//Èë¶È
// cout<<i<<indegree[i]<<";";
for(int t=0; t<indegree[i]; t++)
{
G[(hashpos+t+N)%N].push_back(i);
}
}
TopSort(N);
cout<<Ans[0];
for(int i=1; i<Ans.size(); i++)
{
cout<<" "<<Ans[i];
}
delete hash;
delete indegree;
delete G;
return 0;
}