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. vue引入bootstrap、jquery

    在进行vue的学习,项目中需要引入bootstrap.jquery的步骤. 一.引入jQuery 在当前项目的目录下(就是package.json),运行命令 cnpm install jquery ...

  2. 五、Hexo静态博客背景及界面显示优化配置

    示例预览:我的主页 背景图片添加 自动切换背景 静态本地背景 首先将已选定的背景图片放到博客根目录下的\source\images下 ​ 示例:D:\Blog\source\images\backgr ...

  3. [cf 1264 C] Beautiful Mirrors with queries

    题意: 你有$n$个魔镜,第$i$个魔镜有$p_{i}$的概率说你美. 从第1天开始,你会依次询问魔镜$1-n$你美不美. 若第$i$个魔镜说你美则你明天会继续询问第$i+1$个魔镜. 否则你明天会从 ...

  4. 图解微信小程序---调用API操作步骤

    图解微信小程序---调用API操作步骤 什么是API API(Application Programming Interface,应用程序编程接口:是一些预先定义的函数,目的是提供应用程序与开发人员基 ...

  5. tornado6与python3.7,异步新姿势

    废话不多说,直接上代码 __auth__ = "aleimu" __doc__ = "学习tornado6.0+ 版本与python3.7+" import t ...

  6. 【spring boot】注解@Slf4j的使用

    注解@Slf4j的使用 如果不想每次都写 private final Logger logger = LoggerFactory.getLogger(当前类名.class); 可以用注解@Slf4j  ...

  7. MSSQL 生成唯一自增数据的办法

    我的应用场景是多进程并发获取这个计数,且要保证唯一且自增,我用的办法是锁表 计数表Counter,就一行数据 下面是存储过程 create procedure [dbo].[GetCount] AS ...

  8. Python学习笔记之将数据写入到文件中

    10-3 访客:编写一个程序,提示用户输入其名字:用户作出响应后,将其名字写入到文件guest.txt 中. 编写Python代码: username = input("Please ent ...

  9. 不要在 MySQL 中使用“utf8”,请使用“utf8mb4”

    不要在 MySQL 中使用“utf8”,请使用“utf8mb4” 最近我遇到了一个bug,我试着通过Rails在以“utf8”编码的MariaDB中保存一个UTF-8字符串,然后出现了一个离奇的错误: ...

  10. mysql后台线程详解

    1.mysql后台线程 mysql后台线程主要用于维持服务器的正常运行和完成用户提交的任务,主要包括:master thread,read thread,write thread,redo log t ...