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
/*知道散列数组大小,和散列数组值反求输入数组*/
#include<cstdio>
#include<cstdlib>
#include<set>
using namespace std;
const int maxn = ;
int G[maxn][maxn]; void BuildGraph(int hash[], int n);
void TopSort(int hash[], int hashMap[], int n, int num); int main()
{
int n;
int num = ; //纪录hash表中元素个数
int hash[maxn], hashMap[maxn];
scanf("%d", &n); for (int i = ; i < n; i++)
{
scanf("%d", &hash[i]);
hashMap[hash[i]] = i;
if (hash[i] >= )
{
num++;
}
} BuildGraph(hash, n);
TopSort(hash, hashMap, n, num);
return ;
} void BuildGraph(int hash[], int n)
{
for (int i = ; i < n; i++)
{
if (hash[i] >= )
{
int tmp = hash[i] % n; //对应采用线性检查法解决碰撞的元素
if (hash[tmp] != hash[i])
{
for (int j = tmp; j != i; j = (j+) % n) //建立有向拓扑图来
{
G[j][i] = ; //凡是在这个元素之前的位置,都要依赖他们先输入
}
}
}
}
} void TopSort(int hash[], int hashMap[], int n, int num)
{
int cnt = ;
int Indegree[maxn] = {};
set<int> s; for (int v = ; v < n; v++)
{
for(int w = ; w < n; w++)
{
if (G[v][w] != )
{
Indegree[w]++;
}
}
} for (int i = ; i < n; i++)
{
if (Indegree[i] == && hash[i] > )
{
s.insert(hash[i]);
}
} while (!s.empty())
{
int now = hashMap[*s.begin()];
s.erase(s.begin());
cnt++;
printf("%d", hash[now]);
if (cnt != num)
{
printf(" ");
} for (int v = ; v < n; v++)
{
if (G[now][v] != )
{
if (--Indegree[v] == )
{
s.insert(hash[v]);
}
}
}
}
}

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 11-散列4 Hard Version (30分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/680 5-18 Hashing - Hard Version   (30分) Given ...

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

  4. JavaScript数据结构-11.散列

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. 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 us ...

  6. 11-散列4 Hashing - Hard Version

    题目 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 基本思路 ...

  7. 纯数据结构Java实现(11/11)(散列)

    欢迎访问我的自建博客: CH-YK Blog.

  8. 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 Bon ...

  9. 散列(Hash)表入门

    一.概述 以 Key-Value 的形式进行数据存取的映射(map)结构 简单理解:用最基本的向量(数组)作为底层物理存储结构,通过适当的散列函数在词条的关键码与向量单元的秩(下标)之间建立映射关系 ...

随机推荐

  1. 如何解决aws解绑银行卡问题?

    首先先来说明一下我自己的情况? 一年的免费使用 前提:没有开启任何的实例服务 先贴一条官方的解释 关于我小白一个.学校课程要求使用aws,注册之后在网络上看到一堆人踩坑,aws的扣费就是个坑! 预授权 ...

  2. 通过消息总线Spring Cloud Bus实现配置文件刷新(使用Kafka或RocketMQ)

    如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用webhook的机制每次提交代码发送请求来刷新客户端,当客户端越来越多的时候,需要每个客户端都执行一遍,这种方案就不太适合了.使用 ...

  3. Thinkpad S430 3364-A59 (笔记本型号)加内存条过程

    背景:13年初选购的本,目前使用发现卡得厉害,原装内存只有4G,遂寻思扩充内存. [1]准备工具 待准备工具明细: (1)螺丝刀 主要为了卸后盖的螺丝. (2)内存条 待增加的内存条,如下图: 说明一 ...

  4. 【LeetCode】145. Binary Tree Postorder Traversal

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...

  5. 2019-11-29-WPF-开启-ScrollViewer-的触摸滚动

    原文:2019-11-29-WPF-开启-ScrollViewer-的触摸滚动 title author date CreateTime categories WPF 开启 ScrollViewer ...

  6. .net core ajax使用EPPlus上传excle导入总结

    前端 <form class="layui-form" id="div_imp" style="display:none;"> ...

  7. Markdown温故知新(0):导航目录

    Markdown温故知新(0):导航目录 Markdown温故知新(1):Markdown面面观 Markdown温故知新(2):详解七大标准语法 Markdown温故知新(3):六个实用扩展语法 M ...

  8. 线程状态---Day24

    线程状态概述: 当线程被创建并启动以后,它既不是一启动就进入了执行状态,也不是一直处于执行状态.在线程的生命周期中, 有几种状态呢?在API中 java.lang.Thread.State 这个枚举中 ...

  9. ES6环境搭配(一)

    一.Node(NodeJS.Node.js)的安装:1.下载官网下载地址:https://nodejs.org/en/ 2.安装a.Linux先将安装包解压,然后进行环境变量的配置即可b.window ...

  10. Object-C学习之NSSet和NSMutableSet

    转自:http://blog.csdn.net/likandmydeer/article/details/7939749 一.简介 集合(set)是一组单值对象,它可以是固定的(NSSet).也可以是 ...