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 N (≤), 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

发现元素的输入有先后关系,比如1,13必须先12输出。所以选用拓扑排序

  1 #include <cstdio>
  2 #include <stdlib.h>
  3 #define MAXVERTEXNUM 1001
  4 #define INFINITY 65536
  5 #define ElementType DataAndPos
  6 #define MINDATA -100
  7
  8 typedef int Vertex;
  9 typedef int Position;
 10
 11
 12 struct HashNode {
 13     Vertex i;
 14     int Data;
 15 };
 16 typedef struct HashNode * DataAndPos;
 17
 18 struct GNode {
 19     int Nv;
 20     int G[MAXVERTEXNUM][MAXVERTEXNUM];
 21     int Data[MAXVERTEXNUM];
 22 };
 23 typedef struct GNode * MGraph;
 24
 25 struct HNode {
 26     ElementType Data[MAXVERTEXNUM];
 27     int Size;
 28 };
 29 typedef struct HNode * MinHeap;
 30
 31
 32 //Function Area
 33 MGraph buildGraph();
 34 MGraph createGraph();
 35
 36 int TopSort(MGraph G, Vertex TopOrder[]);
 37 MinHeap createMinHeap();
 38 void Insert(MinHeap H, Vertex W, MGraph Graph);
 39 Vertex DeleteMin(MinHeap H);
 40
 41 void ShowArray(int A[], int N);
 42
 43
 44
 45 int main()
 46 {
 47     int TopOrder[MAXVERTEXNUM];
 48     int RealNum;
 49
 50     MGraph G = buildGraph();
 51     RealNum = TopSort(G,TopOrder);
 52     ShowArray(TopOrder, RealNum);
 53
 54 }
 55
 56
 57 MGraph buildGraph()
 58 {
 59     MGraph Graph = createGraph();
 60     Vertex P;
 61
 62     for (P=0; P<Graph->Nv; P++) {
 63         if (Graph->Data[P] > 0) {
 64             if (Graph->Data[P] % Graph->Nv != P) {
 65                 for (Vertex W = Graph->Data[P] % Graph->Nv; W!=P; W = (W + 1) % Graph->Nv) {
 66                     Graph->G[W][P] = 1;
 67                 }
 68             }
 69         }
 70
 71     }
 72
 73
 74     return Graph;
 75
 76
 77
 78
 79
 80 }
 81 MGraph createGraph()
 82 {
 83     MGraph Graph;
 84     Graph = (MGraph) malloc( sizeof(struct GNode) );
 85
 86     scanf("%d", &Graph->Nv);
 87
 88     for (int i=0; i<Graph->Nv; i++) {
 89         for (int j=0; j<Graph->Nv; j++) {
 90             Graph->G[i][j] = INFINITY;
 91         }
 92     }
 93
 94     for (int i=0; i<Graph->Nv; i++) {
 95         scanf("%d", &(Graph->Data[i]) );
 96     }
 97     return Graph;
 98 }
 99
100 int TopSort( MGraph Graph, Vertex TopOrder[] )
101 {
102     int Indegree[MAXVERTEXNUM];
103     int cnt;
104     cnt = 0;
105
106     Vertex V, W;
107     MinHeap H = createMinHeap();
108
109
110     for (W = 0; W<Graph->Nv; W++) {
111         if (Graph->Data[W] > 0) {
112             Indegree[W] = 0;
113         }
114         else Indegree[W] = -1;
115
116     }
117     for (V = 0; V<Graph->Nv; V++) {
118         for (W=0; W<Graph->Nv; W++) {
119             if (Graph->G[V][W] == 1) {
120                 Indegree[W]++;
121             }
122         }
123     }
124     for (V = 0; V<Graph->Nv; V++) {
125         if (Indegree[V] == 0) {
126             Insert(H, V, Graph);
127         }
128     }
129
130
131     cnt = 0;
132     while (H->Size != 0) {
133         V = DeleteMin(H);
134         TopOrder[cnt++] = Graph->Data[V];
135
136         for (W=0; W<Graph->Nv; W++) {
137             if (Graph->G[V][W] == 1) {
138                 if (--Indegree[W] == 0) {
139                     Insert(H, W, Graph);
140                 }
141             }
142
143         }
144
145     }
146
147     return cnt;
148
149
150
151 }
152 MinHeap createMinHeap()
153 {
154     MinHeap H = new HNode;
155     H->Size = 0;
156     DataAndPos Sentry = new HashNode;
157     Sentry->Data = MINDATA;
158     H->Data[0] = Sentry;
159
160     return H;
161 }
162 void Insert(MinHeap H, Vertex W, MGraph Graph)
163 {
164     DataAndPos NewNode = new HashNode;
165     NewNode->Data = Graph->Data[W];
166     NewNode->i = W;
167
168     int i;
169
170     for (i = ++H->Size; H->Data[i/2]->Data > NewNode->Data ; i/=2) {
171         H->Data[i] = H->Data[i/2];
172     }
173     H->Data[i] = NewNode;
174
175 }
176
177 Vertex DeleteMin(MinHeap H)
178 {
179     ElementType first;
180     ElementType last;
181     int Parent, Child;
182
183     first = H->Data[1];
184     last = H->Data[H->Size--];
185     for (Parent = 1; Parent * 2 <= H->Size; Parent = Child ) {
186         Child = Parent * 2;
187         if (Child < H->Size and H->Data[Child]->Data > H->Data[Child+1]->Data ) {
188             Child++;
189         }
190         if (H->Data[Child]->Data > last->Data) {
191             break;
192         }
193         else H->Data[Parent] = H->Data[Child];
194     }
195     H->Data[Parent] = last;
196
197     return first->i;
198 }
199
200 void ShowArray(int A[], int N)
201 {
202     for (int i=0; i<N; i++) {
203         if(i!=0) printf(" ");
204         printf("%d", A[i]);
205     }
206     printf("\n");
207 }

 

原文地址:https://www.cnblogs.com/acoccus/p/10935801.html

时间: 2024-11-14 12:44:28

11-散列4 Hashing - Hard Version (30 分)的相关文章

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

题目链接: Hashing-Hard Version 解题思路: 暴力,首先根据题目要求将所有给出节点按字典序(从小到大)排列 循环S(节点总数)次,每次通过遍历数组找到一个未出现过的,且满足条件:只能出现在现有位置上(本可出现在之前位置,但被其他节点占据)的节点 输出节点的值 并标记为出现过即可得到答案 代码: #include<iostream> #include<cstdio> #include<cstring> #include<algorithm>

PTA 07-图5 Saving James Bond - Hard Version (30分)

07-图5 Saving James Bond - Hard Version   (30分) This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was captured by a group of drug dealers. He was sent to a small piece of lan

7-11 Saving James Bond - Hard Version (30分)

This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was captured by a group of drug dealers. He was sent to a small piece of land at the center of a lake filled with crocodile

数据结构学习笔记07散列查找

1.散列表(Hash) 查找的本质: 已知对象找位置. 有序安排对象:全序.半序 直接“算出”对象位置:散列 时间复杂度几乎是常量:O(1),即查找时间与问题规模无关 散列查找法的两项基本工作: 计算位置:构造散列函数确定关键词存储位置: 解决冲突:应用某种策略解决多个关键词位置相同的问题 散列(Hashing) 的基本思想是: ①以关键字key为自变量,通过一个确定的函数 h(散列函数),计算出对应的函数值h(key),作为数据对象的存储地址. ②可能不同的关键字会映射到同一个散列地址上,即h

散列表之完美散列

散列表之完美散列 完美散列perfect hashing 两级散列法 gperf工具来自动生成完美散列函数 gperf的安装 gperf关键字文件的书写格式 gperf应用举例 注意 本文中的所有代码你都可以在这里: https://github.com/qeesung/algorithm/tree/master/chapter11/11-5/gperf(这里会及时更新) 或者是这里: http://download.csdn.net/detail/ii1245712564/8936303 找到

数据结构--散列排序--散列表

散列表 散列查找,我们又回到了查找, 编译的时候,涉及变量及属性的管理: 插入:新变量的定义 查找:变量的引用 实际上是动态查找问题,查找树AVL树. 两个变量名(字符串)比较效率不高.字符串的比较要一个一个的比下去,时间会比较长, 是否可以把字符串转换成数字,再处理,就快多了.就是散列查找的思想. 已知的查找方法: 顺序查找                                          O(N) 二分查找(静态查找,不适合动态查找)   O(log2N) 二叉搜索数    

进阶实验8-2.1 逆散列问题 (30分)

给定长度为 N 的散列表,处理整数最常用的散列映射是 (.如果我们决定用线性探测解决冲突问题,则给定一个顺序输入的整数序列后,我们可以很容易得到这些整数在散列表中的分布.例如我们将 1.2.3 顺序插入长度为 3 的散列表HT[]后,将得到HT[0]=3,HT[1]=1,HT[2]=2的结果. 但是现在要求解决的是“逆散列问题”,即给定整数在散列表中的分布,问这些整数是按什么顺序插入的? 输入格式: 输入的第一行是正整数 N(≤1000),为散列表的长度.第二行给出了 N 个整数,其间用空格分隔

2020年2月24日09:06:11,Hash散列

问题描述 /** 有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,住址..),当输入该员工的id时,* 要求查找到该员工的所有信息.* ?要求: 1)不使用数据库,,速度越快越好=>哈希表(散列)* 2)添加时,保证按照id从低到高插入[课后思考:如果id不是从低到高插入,但要求各条链表仍是从低到高,怎么解决?]* 3)使用链表来实现哈希表, 该链表不带表头** */ 代码实现 package day0223 /* * 有一个公司,当有新的员工来报道时,要求将该员工的

散列查找的查找插入及冲突处理方法

处理冲突的方法 1.换个位置:开放地址法 2.同一位置的冲突对象组织在一起:链地址法 开放地址法(Open Addressing): 一旦产生了冲突(该地址已有其他元素),就按某种规则去寻找另一空地址 若发生了第i次冲突,试探的下一个地址将增加di, 基本公式: hi(key) = (h(key)+di) mod TableSize (1≤i<TableSize) di决定了不同解决冲突方案:线性探测.平方探测.双散列 线性探测:di = i +1 +2 +3 平方探测:di = ±i^2 +1