09-散列3. Hashing - Hard Version (30)

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

提交代


http://bbs.csdn.net/topics/320136696

解读在这。。。。。。。。

转自http://blog.sina.com.cn/s/blog_ce1b01420102vizs.html

想不明白就想不明白了,囧。




  从表中可以得出,冲突=下标 - Key % N, 但32的下标小于Key%N, 为了使计算一致,补上N使它也符合一般情况,所以冲突计算的公式为:
冲突 = (下标 - Key % N + N) % N 
  冲突是因为散列函数求出来的位置被占了,移到下个位置的时候,如果被占,又冲突。换个角度说,冲突是被其他数影响的,被2个数影响冲突就是2,被3个数影响,冲突就是3,依次类推。画成图后(本来线用了不同的颜色,QQ截图被压缩了),冲突可以看成边,那么要还原成最小输入序列就变成了拓扑排序。
解题思路:
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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值