PTA—11-散列4 Hashing - Hard Version (30分)
来源:互联网 发布:科技狗一元云购源码 编辑:程序博客网 时间:2024/05/01 03:23
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写算法。正如陈越老师说的,这道题出的非常巧妙,结合了散列和拓扑排序的知识,最后还用优先队列来实现。
说下这道题目的注意点:
1.建立邻接表的话我是采用了VECTOR类来实现,大家可以在网上找找资料有很多我就不叙述了。
2.hashtable的Key和Key所在的位置要对应起来,我是采用了struct的结构来储存
// PTA——Hashing - Hard Version(拓扑排序的经典!).cpp : Defines the entry point for the console application.//#include <vector>#include <queue>#include<iostream>#include <functional>using namespace std;struct HashNode{int Hash;int Key;friend bool operator< (HashNode n1, HashNode n2){ return n1.Key > n2.Key; }};int* Indegree; //用来储存各个key对应的入度HashNode* pHashNode;void ToplogicSort(vector<vector<int>> rating,int num){vector<int>TopNum;int i; priority_queue<HashNode>Q;for(i=0;i<num;i++)if(!Indegree[i])Q.push(pHashNode[i]);while(!Q.empty()){HashNode h=Q.top();int V=h.Hash;TopNum.push_back(h.Key);Q.pop();for(i=0;i<rating[V].size();i++)if(--Indegree[rating[V][i]]==0)Q.push(pHashNode[rating[V][i]]);}printf("%d",TopNum[0]);for(i=1;i<TopNum.size();i++)printf(" %d",TopNum[i]);}int main(){int i,j,num,currentpos,hashpos; scanf("%d",&num);vector<vector<int>> rating(num); //用vector来模拟邻接表pHashNode=new HashNode[num];Indegree=new int[num];for(i=0;i<num;i++){scanf("%d",&pHashNode[i].Key);pHashNode[i].Hash=i;if(pHashNode[i].Key==-1){Indegree[i]=-1;continue;}currentpos=i;hashpos=pHashNode[i].Key%num; //根据hash函数计算出来的位置Indegree[i]=(currentpos-hashpos+num)%num;//计算发生冲突的次数,即入度数for(j=0;j<Indegree[i];j++)rating[(hashpos+j+num)%num].push_back(i); //建立邻接表,把从通过函数计算出的位置到当前位置前的点的vector里都加上当前点的下标}ToplogicSort(rating,num);return 0;}
- PTA—11-散列4 Hashing - Hard Version (30分)
- 11-散列4 Hashing - Hard Version
- PTA—11-散列4 Hashing
- PTA 11-散列2 Hashing (25分)
- PAT 09-散列3. Hashing - Hard Version (30)
- 09-散列3. Hashing - Hard Version (30)
- 09-散列3. Hashing - Hard Version (30)
- PTA 11-散列2 Hashing
- PAT 数据结构 09-散列3. Hashing - Hard Version (30) 拓扑排序
- PAT--Hashing - Hard Version (30)
- 11-散列2 Hashing (25分)
- 11-散列2 Hashing (25分)
- 11-散列2 Hashing (25分)
- 11-散列2 Hashing (25分)
- 11-散列4 Hashing
- 09-3. Hashing - Hard Version
- 5-11 Saving James Bond - Hard Version (30分)
- 11-散列4 Hashing
- URL知识
- 实习入职第四天:编译clean文件时Windows提示需要管理员权限
- Android JNI 开发
- 除了技术,我们还能拼什么?
- javascript-下拉菜单的实现
- PTA—11-散列4 Hashing - Hard Version (30分)
- Java 8简明教程
- Jetty和Tomcat的选择:按场景而定
- 剑指offer(三十二)之把字符串转换成整数
- Yii框架中事件概念
- UIApplication的详细介绍
- C++运算符优先级
- webpack+vue+bootstrap项目实践
- 筹备