5-18 Hashing
来源:互联网 发布:网络数据库的信息检索 编辑:程序博客网 时间:2024/04/24 21:29
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 (≤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:
1133 1 13 12 34 38 27 22 32 -1 21
Sample Output:
1 13 12 21 33 34 38 27 22 32
参考了其他人的代码,发现他们都用了STL,之前我从来没有用过。用了才知道STL真的方便,这道题可以用STL的优先队列,免去了自己写堆的过程了。
这是数据结构课里最后一题了,也断断续续把数据结构学完了,感觉自己有很多东西都不知道,但也学到了很多东西。
学习的过程中写不出来,没弄懂的感觉太难受了,有时候也怪自己偷懒了。但大部分还是一点一点理解弄懂了,做出来题的时候是最开心的时候了。
以后的学习中还是要坚持,再难都要啃下来!
编程这东西还是要靠自己多看多写多练啊!
#include<stdio.h> #include<stdlib.h> #include<math.h>#include<string.h>#include<algorithm>#include <vector> #include <queue> #include<iostream> #include <functional> #define MAX 1001#define MAXD 6#define TELNUM 10using namespace std;struct HashNode{int hash;int key;friend bool operator < (HashNode a,HashNode b)/*结构体的优先级设置*/{return a.key > b.key;}};typedef struct HashNode *pHashNode;int Indegree[MAX];void TopSort(vector<vector<int> > rating,int N,pHashNode H){vector<int>TopNum;int i;priority_queue<HashNode> Q;/*利用优先队列*/for(i = 0; i < N; i++){if(!Indegree[i]){Q.push(H[i]);}}while(!Q.empty()){HashNode h = Q.top();int V = h.hash;TopNum.push_back(h.key);Q.pop();for(int i = 0; i < rating[V].size(); i++){if(--Indegree[rating[V][i]] == 0){Q.push(H[rating[V][i]]);}}}printf("%d",TopNum[0]);for(int i = 1 ; i < TopNum.size(); i++){printf(" %d",TopNum[i]);}}int main(void){int N;int i,j,hashpos,currentpos;scanf("%d",&N);vector<vector<int> > rating(N);/*模拟邻接表*/pHashNode H = (pHashNode)malloc(N * sizeof(struct HashNode));for(i = 0; i < N; i++){scanf("%d",&H[i].key);H[i].hash = i;/*每个元素对应一个下标*/if(H[i].key == -1){Indegree[i] = -1;continue;}currentpos = i;hashpos = H[i].key % N;Indegree[i] = (currentpos - hashpos + N) % N;/*计算发生冲突的次数,相当与一个顶点的入度*/for(j = 0; j < Indegree[i]; j++)/*找到每个冲突的地方(指向H[i].key元素的那个元素),把i接入指向每个发生冲突的地方*/{rating[(hashpos + j + N) % N].push_back(i);/*rating[X]代表表头,它里面的元素代表相连的元素*/}}TopSort(rating,N,H);return 0;}
- 5-18 Hashing
- 5-18 Hashing
- !!!Chapter 5 Hashing
- Hashing
- Hashing
- Hashing
- Hashing
- Hashing
- Hashing
- 5-21 Hashing (25分)
- 数据库5 索引 动态哈希(Dynamic Hashing)
- 关于hashing
- Consistent hashing
- Cuckoo Hashing
- Consistent Hashing
- Consistent Hashing
- Extensible Hashing
- Consistent Hashing
- shell脚本之函数
- 计算n!
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(十)SVN搭建
- 图解DNS的工作原理
- MQTT---HiveMQ源码详解(十九)Cluster-Request/Response
- 5-18 Hashing
- 创建Activiti项目
- ACM程序设计 -L (字符串倒输出)
- MQTT---HiveMQ源码详解(二十)Cluster-Replicate/VectorClock
- 三本书--前端好书
- JavaScript基础
- SG函数与组合博弈问题
- Java JDK安装与环境变量配置
- JSON格式转换成map并根据key值取值