pat09-散列3. Hashing - Hard Version (30)
09-散列3. Hashing - Hard Version (30)
Given a hash table of size N, we can define a hash function H(x) = x%N. 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:
11 33 1 13 12 34 38 27 22 32 -1 21
Sample Output:
1 13 12 21 33 34 38 27 22 32
提交代码
主要思路:
哈希表规模为n。对于输入的位置编号为i的元素a,如果:
1.i==a%n。说明a放i位置不是由于冲突,将a放入优先队列。
2.i>a%n。如果编号为a%n到n-1的元素之前已经填入哈希表(h1[j].exist=true),则第i个元素可以进入优先队列。否则,如果编号为a%n到n-1的元素在位置编号为i的元素填入之后填入哈希表,意味着位置编号为i的元素插入哈希表时,发生冲突向后转移不可能到达位置i,而应该在i位置之前填入,矛盾。
3.i<a%n。与2同理。
1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<queue> 6 using namespace std; 7 struct node{ 8 bool exist; 9 node(){ 10 exist=false; 11 } 12 }; 13 node h1[1005]; 14 priority_queue< pair<int,int>,vector<pair<int ,int> >,greater<pair<int,int> > > pq;//记录值和放的位置,按pair的第一个元素升序排列 15 pair<int,bool> input[1005];//标记元素有没有进入队列 16 queue<int> q; 17 int main(){ 18 //freopen("D:\\INPUT.txt","r",stdin); 19 int n,num,i,count=0; 20 scanf("%d",&n); 21 for(i=0;i<n;i++){ 22 scanf("%d",&num); 23 input[i]=make_pair(num,false); 24 if(num>=0){//记录不等于负数的元素个数 25 count++; 26 } 27 } 28 int j,k,post; 29 for(i=0;i<count;i++){//复杂度n^2:扫数列count次,每次将当前满足条件的最小元素进入队列q 30 for(j=0;j<n;j++){//遍历数列的每个元素 31 if(!input[j].second&&input[j].first>=0){//寻找没有进入队列并且不等于-1的元素 32 post=input[j].first%n;//元素本来应该在哈希表中的位置 33 if(post==j){//本来要放的位置和现在在哈希表中的位置相同 34 pq.push(make_pair(input[j].first,j)); 35 //h1[post].exist=true;//标记 36 input[j].second=true; 37 continue; 38 } 39 if(post<j){//本来要放的位置在现在的位置前面 40 for(k=post;k<j;k++){ 41 if(!h1[k].exist){//前面没有元素填充 42 break; 43 } 44 } 45 if(k==j){//可以进队pq 46 pq.push(make_pair(input[j].first,j)); 47 //h1[j].exist=true; 48 input[j].second=true; 49 } 50 } 51 else{//post>j 52 for(k=post;k<n;k++){//先由post开始向后遍历 53 if(!h1[k].exist){ 54 break; 55 } 56 } 57 if(k==n){//满足条件后,由0开始向j遍历 58 for(k=0;k<j;k++){ 59 if(!h1[k].exist){ 60 break; 61 } 62 } 63 if(k==j){ 64 pq.push(make_pair(input[j].first,j)); 65 //h1[j].exist=true; 66 input[j].second=true; 67 } 68 } 69 } 70 } 71 } 72 pair<int,int> top=pq.top(); 73 pq.pop(); 74 q.push(top.first);//找到本次满足条件:在队列pq且最小的元素,进入输出队列q 75 h1[top.second].exist=true;//意味着top.second位置已经放了元素 76 } 77 printf("%d",q.front());//对输出队列q进行输出 78 q.pop(); 79 while(!q.empty()){ 80 printf(" %d",q.front()); 81 q.pop(); 82 } 83 return 0; 84 }
转载于:https://www.cnblogs.com/Deribs4/p/4731257.html
pat09-散列3. Hashing - Hard Version (30)相关推荐
- 数据结构PTA习题:11-散列4 Hashing - Hard Version逆散列问题 (30分)——散列+拓扑排序
11-散列4 Hashing - Hard Version 逆散列问题 (30分) Given a hash table of size N, we can define a hash functio ...
- 区块链基础:散列法 (Hashing)
灯泡,比特(bits)与字节(bytes) 你可能知道计算机中所有的数据都是由0或1组成的,最小的数据单位就是一个比特(bit,或位),它也是0或者1.想象一下,一台计算机拥有着很多的灯泡,而这个灯泡 ...
- 11-散列4 Hashing - Hard Version(2种方法)
题目描述 Given a hash table of size N, we can define a hash function H(x)=x%N. Suppose that the linear p ...
- PTA数据结构题目集 第十一周——散列查找
目录 11-散列1 电话聊天狂人 (25分) 思路 代码 测试点 11-散列2 Hashing (25分) 思路 代码 测试点 11-散列3 QQ帐户的申请与登陆 (25分) 题目大意 思路 代码 测 ...
- 【数据结构笔记39】哈希表/散列表、(数据关键字/字符串关键字)散列构造函数
本次笔记内容: 11.1.1 引子:散列的基本思路 11.1.2 什么是散列表 11.2.1 数据关键词的散列函数构造 11.2.2 字符串关键词的散列函数构造 文章目录 散列表背景 基本思想引出 已 ...
- Python 进阶——标准散列机制
散列(hashing)技术不仅在绝大多数算法类书籍中都有详细介绍,而且在 Python 程序当中也非常普及.该技术往往会涉及一些经由某既定对象计算而来的整数值(乍看之下似乎是随机的).例如,我们可以用 ...
- 数据结构与算法——17. 散列(哈希)与完美散列函数
文章目录 一.散列(Hashing)的概念 举例说明 二.完美散列函数 1. 数据的一致性校验 2. 完美散列函数用于数据的一致性校验 一.散列(Hashing)的概念 在查找算法中,如果数据项之间是 ...
- 【散列】杜鹃散列详情与C++实现代码
导引 在球-箱问题中,如果将N项随机抛入N个箱子中,那么含球最多的箱子的期望球数为Θ(logN/log logN). 如果在每次投掷中随机选取两个箱子且将被投项投入(在那一刻)较空的箱子中,则最大箱子 ...
- 数据结构37:什么是散列
目录 一.散列:Hashing 二.散列表:基本概念 三.散列:示例 一.散列:Hashing 前面我们利用数据集中关于数据项之间排列关系的知识,来将查找算法进行了提升. 如果数据项之间是按大小排好序 ...
最新文章
- 开启windows ping端口功能
- Hadoop伪集群环境搭建
- Windows 10 之修改登录背景(Win10BGChanger)
- vs2010变的特别卡解决办法
- php 读xml的两种方式
- 【转】AsyncTask的用法
- Scratch1.4案例:射日小游戏
- JAVA对接阿里语音识别引擎
- VMware Horizon view 7 云桌面终端安全解决方案
- word文档打对勾_word文档怎么打勾 word文档方框内打勾六种方法介绍
- 写给小白的Python之006:数据类型之列表
- mongodb类型转换
- ubuntu16.04 安装微信客户端
- SpringBoot+Hibernate配置
- 设置行与行的间隔(行间距)
- 【自控原理】第四章 根轨迹法
- BERT-BiLSTM-CRF模型代码
- mybatis从入门到精通(刘增辉著)-读书笔记第二章
- 华为路由器:WLAN直连式三层组网实验
- vb.net在自己的应用中嵌入谷歌地球的方法
热门文章
- 允许匿名用户访问VisualSVN
- DataReader终结篇
- 蓝桥杯2020答案c语言b组,2020十月份蓝桥杯B组省赛题解大全(害!附题面文件和部分代码~)...
- 输入5个整形数据_妙招技法:Excel表格数据录入的5个小技巧
- 电脑显示器闪屏_时尚超薄可升降:华硕新品家用护眼显示器MZ27AQL
- php正则如何使用 1,PHP正则表达式使用详解(1)
- 西南交通大学计算机网络,西南交通大学计算机网络实验2015-2016第2学期期末试卷...
- acm java 类库_ACM java入门和基本技巧
- linux history 用法,Linux之History的使用
- mysql三大范式_数据库的三大范式?