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.
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 分)的更多相关文章
- pat09-散列3. Hashing - Hard Version (30)
09-散列3. Hashing - Hard Version (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 HE, Qin ...
- PTA 11-散列4 Hard Version (30分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/680 5-18 Hashing - Hard Version (30分) Given ...
- 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 ...
- JavaScript数据结构-11.散列
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 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 ...
- 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 基本思路 ...
- 纯数据结构Java实现(11/11)(散列)
欢迎访问我的自建博客: CH-YK Blog.
- 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 ...
- 散列(Hash)表入门
一.概述 以 Key-Value 的形式进行数据存取的映射(map)结构 简单理解:用最基本的向量(数组)作为底层物理存储结构,通过适当的散列函数在词条的关键码与向量单元的秩(下标)之间建立映射关系 ...
随机推荐
- Java 微信支付分对接记录 (先享后付)
微信支付分(先享后付)对接记录: 微信支付分对接步骤 填写开通支付分的申请表格 此步骤大概需要审核 1-3 个工作日; (模板-服务信息配置表-[先享后付免确认]-[商户名].xls) 填写商户信息 ...
- html 四种定位含义
技术过段时间不用的话就会忘记,需要复习一下 1.static:默认值.没有定位,元素出现在正常的流中(忽略top,bottom,left,right或者z-index声明). 2.relative:生 ...
- windows版mysql8.0安装详解
2018年07月04日 13:37:40 Zn昕 阅读数 6433更多 分类专栏: mysql 版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接和本声明. ...
- 分布式任务调度平台XXL-JOB快速使用与问题总结
1.XXL-JOB简介 XXL-JOB is a lightweight distributed task scheduling framework. It's core design goal is ...
- excel中统计COUNTIFS的值为0
excel中统计COUNTIFS的值为0 个人认为是由于导出的文件里面的字符个数问题 使用 =COUNTIFS(H1:H175,"微信支付") 这个的结果居然是0,找了很多办法 于 ...
- USE11 上oracle11导入数据中文乱码
分类专栏: 数据库 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/suqimm/artic ...
- mssql 根据执行计划细节做优化操作
示例: 1.如果select * 通常情况下聚集索引会比非聚集索引更优. 2.如果出现Nested Loops,需要查下是否需要聚集索引,非聚集索引是否可以包含所有需要的列. 3.Hash Match ...
- Bootstrap3-导航
Bootstrap 导航 1. 定义导航组件 基本结构: <!-- 基本导航组件 --> <ul class="nav"> <li class=&qu ...
- 2-Rocketmq产品架构(参考阿里云)
参考链接:https://help.aliyun.com/document_detail/112008.htm
- 10. Javascript 前后端数据加密
为了加强项目的接口安全程度,需求如下 var options = { // 前端需要传送的数据加密 data: { abc: 123, bcd: 123, cds: '撒旦教付货款12313', }, ...