题目:
11-散列4 Hashing - Hard Version (30分)
Given a hash table of size NNN, 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 NNN (≤1000\le 1000≤1000), which is the size of the hash table. The next line contains NNN 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
题意:就是给你一个已经插入结束的哈希表,让你输出插入元素的顺序,如果有多个元素可以插入,那么先插最小值。
思路:哈希拓扑插排还有表排,按照我的思路应该是这么写的,先用哈希计算该元素如果没有冲突应该插入的位置,再在原本的位置与现在的位置之间的区间制造拓扑序,然后根据拓扑序列按照入度从小到大输出,队列中表示可以输出的元素,注意将元素插入队列时,必须保持队列里元素的有序性,因为有多个元素可插入时,元素要从小到大输出所以我们可以在插入的时候顺便插入排序一下。
代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MaxN 10007
typedef struct Node *cn;
int indegree[MaxN]={0};
int Hash[MaxN]={0};
int symbol[MaxN]={0};
int path[MaxN]={0};
int Tsize;
struct Node
{
int Size;
int *ElementType;
};
//
struct line
{
int star;
int end;
int ElementType[MaxN];
}queue;
//
int F(int x)
{
return x%Tsize;
}
void HashAndCount(int x,int nowpos,cn n) //线性探测 ,nowpos代表哈希表目前位置
{
int formerpos=F(x);
if(nowpos>formerpos)
for(int i=formerpos;i<nowpos;i++)
{
n[i].ElementType[n[i].Size++]=nowpos;
indegree[nowpos]++;
}
if(nowpos<formerpos)
{
for(int i=0;i<nowpos;i++)
{
n[i].ElementType[n[i].Size++]=nowpos;
indegree[nowpos]++;
}
for(int i=formerpos;i<Tsize;i++)
{
n[i].ElementType[n[i].Size++]=nowpos;
indegree[nowpos]++;
}
}
}
cn Initialize(int Size) //初始化
{
cn n;
n=(cn)malloc(sizeof(struct Node)*(Size+1));
queue.end=queue.star=0;
for(int i=0;i<Size;i++)
{
n[i].Size=0;
n[i].ElementType=(int *)malloc(sizeof(int)*(Size+1));
}
return n;
}
void InsertAndSort(int pos)
{
symbol[pos]=1;
int Tmp=Hash[pos];
int i;
for(i=queue.end; i>queue.star && Hash[queue.ElementType[i-1]]>Tmp ;i--)
queue.ElementType[i]=queue.ElementType[i-1];
queue.ElementType[i]=pos;
queue.end++;
}
int IsEmptyQ()
{
return queue.end==queue.star;
}
int Delete()
{
return queue.ElementType[queue.star++];
}
int main()
{
cn n;
scanf("%d",&Tsize);
n=Initialize(Tsize);
for(int i=0;i<Tsize;i++)
{
scanf("%d",&Hash[i]);
if(Hash[i]!=-1)
HashAndCount(Hash[i],i,n);
}
for(int i=0;i<Tsize;i++)
{
if(indegree[i]==0 && Hash[i]!=-1)
InsertAndSort(i);
}
while(!IsEmptyQ())
{
int pos=Delete();
printf(" %d",Hash[pos]);
int i;
for(i=0;i<n[pos].Size;i++)
{
indegree[n[pos].ElementType[i]]--;
if(indegree[n[pos].ElementType[i]]==0)
{
path[n[pos].ElementType[i]]=path[pos]+1;
InsertAndSort(n[pos].ElementType[i]);
}
}
}
return 0;
}