陈越何欣民数据结构2017年春11散列4

题目:

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 10001000), 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数据结构第16次作业,hash表 Spellchecking Prerequisites, Goals, and Outcomes Prerequisites: Students should have mastered the following prerequisite skills. • Hash Tables - Understanding of the concept of a recursive function • Inheritance - Enhancing an existing data structure through specialization Goals: This assignment is designed to reinforce the student's understanding of the use of hash tables as searchable containers. Outcomes: Students successfully completing this assignment would master the following outcomes. • Familiarize how to use hash tables, specifically hash sets Background Any word processing application will typically contain a spell check feature. Not only does this feature point out potentially misspelled words; it also suggests possible corrections. Description The program to be completed for this assessment is a spell checker. Below is a screen shot of the program in execution? The program begins by opening a word list text file, specified by a command line parameter. The program outputs an error message and terminates if it cannot open the specified word list text file. A sample word list text file (wordlist.txt) is given in the supplied wordlist.zip archive. After successfully opening the specified word list text file, the program then stores each word into a hash table. The program then opens a file to spell check. This user specifies this file through the command line. After opening this file, the program then compares each word in the file against the words stored in the hash table. The program considers a word to be misspelled if the word does not exist in the hash table. When this occurs, the program displays the line number the word appeared in, the word, and a list of possible corrections. The list of possible corrections for a misspelled word is generated using a simple algorithm. Any variation of a misspelled word that is itself a word (i.e. it is found in the word list file) is a possible correction. Your solution to this asses
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值