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

Given a hash table of size N, 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 (1000), 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:

11
33 1 13 12 34 38 27 22 32 -1 21

Sample Output:

1 13 12 21 33 34 38 27 22 32

解析:如題目中的樣例,假設題目中的元素在一個Hash[11]的數組中

33 % 11 = 0,Hash[0] = 33 ,因此什麼事都不做;

1 % 11 = 1,Hash[1] = 1, 同上;

13 % 11 = 2,Hash[2] = 13, 同上;

12 % 11 = 1,Hash[1] != 12,說明發生了衝突,此時1和13一定在12之前進入哈希表(因爲線性探測法是逐個向後找空位),所以建立<1,12>、<13, 12>這兩條有向邊來表明先後順序;

後面的都類似;

而32 % 11 = 10,Hash[10] != 32,又發生了衝突,而此時已經是表的末尾,因此只能重新回到0號位依次探測空位直到8號位,因此需要建立<21, 32>、<33, 32>、<1, 32>...<22, 32>這些有向邊


以上步驟是根據哈希表來建立一個有向圖,對應BuildGraph函數。其中建立有向圖時,結點我選擇的是用下標來表示(因爲不知道元素的大小),不過還是用了個HashMap數組來記錄下標。

然後就是根據有向圖來做一個拓撲排序,排序的時候使用set容器來存儲入度爲0的元素,方便找到最小的元素。



#include <cstdio>
#include <cstdlib>
#include <set>
using namespace std;
#define MAX 1000
#define INFINITY 0
int A[MAX][MAX];

void BuildGraph ( int Hash[], int N ) {
	int i, j, tmp;
	for ( i = 0; i < N; i++ ) {
		if ( Hash[i] >= 0 ) {
			tmp = Hash[i] % N; //求餘數
			if ( Hash[tmp] != Hash[i]) { //如果這個位置已被佔有
				for ( j = tmp; j != i; j = ( j + 1 ) % N )
					A[j][i] = 1; //位置j到i的元素都在i之前進入哈希表
			}
		}
	}
}

int TopSort( int Hash[], int HashMap[], int N , int num){
	int V, W, cnt = 0, Indegree[MAX] = {0};
	set<int> s;
	//計算各結點的入度
	for( V = 0; V < N; V++ )
		for( W = 0; W < N; W++ )
			if( A[V][W] != INFINITY )
				Indegree[W]++;	//對於有向邊<V,W>累計終點W的入度
	//入度爲0的入隊
	for( int i = 0; i < N; i++ )
		if( Indegree[i] == 0 && Hash[i] > 0 )
			s.insert( Hash[i] );  //將大於0且入度爲0的頂點放入集合,自動排序
	while( !s.empty() ) {
		V = HashMap[ *s.begin() ]; //獲取最小的元素的下標
		s.erase( s.begin() );
		cnt++;
		printf("%d", Hash[V]);
		if ( cnt != num )
			printf(" ");
		for( W = 0; W < N; W++ )
			if( A[V][W] != INFINITY ) {  //<V, W>有有向邊
				if( --Indegree[W] == 0 )  //去掉V後,如果W的入度爲0
					s.insert( Hash[W] );
			}
	}
	if( cnt != num ) return 0;	//如果沒有取出所有元素,說明圖中有迴路
	else return 1;
}

int main () {
	int N, Hash[1005],HashMap[100000], num = 0;
	scanf("%d", &N);
	for ( int i = 0 ;i < N; i++ ) {
		scanf("%d", &Hash[i]);
		HashMap[ Hash[i] ] = i;	//記錄下標
		if ( Hash[i] > 0 ) num++; //記錄哈希表中的元素個數
	}
	BuildGraph( Hash, N );
	TopSort( Hash, HashMap, N ,num );
	system("pause");
	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章