11-散列4 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.

中文版题目:
给定长度为 N 的散列表,处理整数最常用的散列映射是 H(x)=x%N。如果我们决定用线性探测解决冲突问题,则给定一个顺序输入的整数序列后,我们可以很容易得到这些整数在散列表中的分布。例如我们将 1、2、3 顺序插入长度为 3 的散列表HT[]后,将得到HT[0]=3,HT[1]=1,HT[2]=2的结果。
但是现在要求解决的是“逆散列问题”,即给定整数在散列表中的分布,问这些整数是按什么顺序插入的?

输入格式:
输入的第一行是正整数 N(≤1000),为散列表的长度。第二行给出了 N 个整数,其间用空格分隔,每个整数在序列中的位置(第一个数位置为0)即是其在散列表中的位置,其中负数表示表中该位置没有元素。题目保证表中的非负整数是各不相同的。

输出格式:
按照插入的顺序输出这些整数,其间用空格分隔,行首尾不能有多余的空格。注意:对应同一种分布结果,插入顺序有可能不唯一。例如按照顺序 3、2、1 插入长度为 3 的散列表,我们会得到跟 1、2、3 顺序插入一样的结果。在此规定:当前的插入有多种选择时,必须选择最小的数字,这样就保证了最终输出结果的唯一性。

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

散列问题+拓扑排序。
拓扑排序中,存储入度=0的顶点的容器我使用的是数组。每次将所有入度=0的顶点中数据最小的输出,删除输出顶点在图中的所有出边后,若有新增的入度=0的顶点也存入数组。循环往复,直至所有的非负顶点都实现输出。

C语言实现:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define Infinity 65535
struct vertex
{int index;int data;
};
typedef struct vertex * Vertex;
struct graph
{int Ne;int Nv;int * * M;
};
typedef struct graph * Graph;
Graph CreateGraph(int N);
int main()
{int N;scanf("%d", &N);Vertex V;V = (Vertex)malloc(N * sizeof(struct vertex));Graph G;G = CreateGraph(N);int i, j;int t;int cnt = 0;//记录输入的非负元素个数for (i = 0; i < N; i++)//有向图,无权重{scanf("%d", &V[i].data);V[i].index = i;if (V[i].data < 0) { t = i; }else { t = V[i].data % N; cnt++; }while (t != i){G->M[t][i] = 1;t++;if (t == N) { t = t - N; }}}int * Indegree;//顶点入度Indegree = (int *)malloc(N * sizeof(int));//入度初始化for (i = 0; i < N; i++){Indegree[i] = 0;}for (i = 0; i < N; i++){for (j = 0; j < N; j++){if (G->M[j][i] == 1){Indegree[i]++;}}}int * d;//记录所有此刻入度=0的顶点下标d = (int *)malloc(N * sizeof(int));for (i = 0; i < N; i++){d[i] = -1;}for (j = 0, i = 0; i < N; i++)//此刻入度=0的顶点下标存入数组d{if (Indegree[i] == 0){d[j] = i;j++;}}int flag = 0;while (1){int min = Infinity;int x;//入度=0的最小顶点的下标for (i = 0; i < N; i++){if (min < 0 && V[d[i]].data>0){min = V[d[i]].data;x = i;}else if (min >= 0 && min > V[d[i]].data&&V[d[i]].data > 0){min = V[d[i]].data;x = i;}}if (flag == 0) { printf("%d", V[d[x]].data); flag = 1; }else { printf(" %d", V[d[x]].data); }cnt--;//每输出一个cnt--,直至cnt=0if (cnt == 0) { break; }V[d[x]].data = Infinity;for (i = 0; i < N; i++)//删除输出顶点的所有出边,若使某顶点入度=0,则将该顶点存入数组d{if (G->M[d[x]][i] == 1 && Indegree[i] > 0){Indegree[i]--;if (Indegree[i] == 0){d[j] = i;j++;}}}}return 0;
}
Graph CreateGraph(int N)
{int i, j;Graph G;G = (Graph)malloc(sizeof(struct graph));G->Ne = 0;G->Nv = N;G->M = (int * *)malloc(N * sizeof(int *));for (i = 0; i < N; i++){G->M[i] = (int *)malloc(N * sizeof(int));}for (i = 0; i < N; i++){for (j = 0; j < N; j++){G->M[i][j] = 0;}}return G;
}

数据结构PTA习题:11-散列4 Hashing - Hard Version逆散列问题 (30分)——散列+拓扑排序相关推荐

  1. pat09-散列3. Hashing - Hard Version (30)

    09-散列3. Hashing - Hard Version (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 HE, Qin ...

  2. 数据结构PTA习题:进阶实验5-3.2 新浪微博热门话题 (30分)

    进阶实验5-3.2 新浪微博热门话题 (30分) 新浪微博可以在发言中嵌入"话题",即将发言中的话题文字写在一对"#"之间,就可以生成话题链接,点击链接可以看到 ...

  3. mooc浙大数据结构PTA习题之一元多项式的乘法与加法运算

    设计函数分别求两个一元多项式的乘积与和. 输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. ...

  4. 数据结构PTA习题:基础实验4-2.7 修理牧场 (25分)

    基础实验4-2.7 修理牧场 (25分) 农夫要修理牧场的一段栅栏,他测量了栅栏,发现需要N块木头,每块木头长度为整数L​i​​个长度单位,于是他购买了一条很长的.能锯成N块的木头,即该木头的长度是L ...

  5. 数据结构PTA习题:基础实验7-2.3 德才论 (25分)——排序

    基础实验7-2.3 德才论 (25分) 宋代史学家司马光在<资治通鉴>中有一段著名的"德才论":"是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜 ...

  6. 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 ...

  7. 数据结构(Data Structure)(C/C++)PTA习题+课后习题

    课本:<数据结构--从概念到c++实现(第三版)> 第一章 绪论 1.1 判断题 1-1 数据元素是数据的最小单位. F         课本:数据元素是数据的基本单位:构成数据元素的最小 ...

  8. 区块链基础:散列法 (Hashing)

    灯泡,比特(bits)与字节(bytes) 你可能知道计算机中所有的数据都是由0或1组成的,最小的数据单位就是一个比特(bit,或位),它也是0或者1.想象一下,一台计算机拥有着很多的灯泡,而这个灯泡 ...

  9. 【数据结构笔记44】线性探测的散列表的逆问题(拓扑排序的方法)

    本次笔记内容: 习题-HHV算法思路概述 文章目录 哈希逆问题 较难的题Hashing - Hard Version 算法示例 哈希逆问题 较难的题Hashing - Hard Version 如上图 ...

最新文章

  1. 最新县及县以上行政区划代码(截止2010年12月31日)
  2. python是面向过程的吗_Python开发是面向过程、函数还是对象?
  3. 我们究竟还要学习哪些Android知识?完整版开放下载
  4. java线程中的task_Java线程(四):Timer和TimerTask
  5. 文本框 价格 保留两位小数 讨论
  6. java.lang.NoClassDefFoundError: org/apache/http/ssl/TrustStrategy 错误解决办法
  7. jmeter debug sample不在查看结果树中显示_Jmeter线程组间传递参数
  8. 相片打印机原理_照片打印机 技术原理介绍_照片打印机_办公打印评测试用-中关村在线...
  9. 51Talk-Level 7 Unit 2 L3
  10. Counting swaps
  11. 电脑重新安装了有线网卡驱动,并且能上网,但网络连接图标有红叉怎么去掉
  12. android 查看cpu 工具6,Android 之CPU监控命令
  13. socket服务器区分各个客户端信息,socket服务器如何区分哪个客户端
  14. JAVA RPG游戏
  15. sklearn.exceptions.NotFittedError: Estimator not fitted, call fit before exploiting the model.
  16. 解决docker容器因报错无法启动的问题,检查、修复容器错误并重启
  17. B样条基函数的定义及系数的意义
  18. 网络安全怎么学?20年白帽子老江湖告诉你
  19. 凯撒密文的破解编程实
  20. 靶场covfefe CTF之内网渗透(一)

热门文章

  1. html修改鼠标右键,电脑鼠标右键菜单内容怎么修改设置
  2. 分享 2021 豆瓣年度榜单电影/图书
  3. 周易起名大师 v18.0算法分析
  4. 计算机一级2017.6,2017年全国计算机一级考试真题
  5. PC一键弹出添加QQ群界面
  6. 一文解析“硬科技、深科技、黑科技”
  7. navicat无法连接远程mysql数据库_[数据库]Navicat Premium 解决无法连接远程mysql数据库问题...
  8. 【Python3解析二维码】翻遍全网找到 2 款库推荐给大家~
  9. 有理函数积分的一般解法
  10. JAVA面向对象课堂总结