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)结构 简单理解:用最基本的向量(数组)作为底层物理存储结构,通过适当的散列函数在词条的关键码与向量单元的秩(下标)之间建立映射关系 ...
随机推荐
- [转帖]SQL Server DBCC命令大全
SQL Server DBCC命令大全 原文出处:https://www.cnblogs.com/lyhabc/archive/2013/01/19/2867174.html DBCC DROPC ...
- Sitecore个性化 - 基础知识
许多组织选择Sitecore作为其高级个性化功能的网站平台.Sitecore个性化需要什么以及它能为您的品牌提供什么? 今天, 对于希望提供更好的客户体验的组织来说,个性化不仅仅是一个很好的选择 - ...
- 用vscode开发vue应用
阅读 3237 收藏 205 2019-05-02 原文链接:segmentfault.com 云服务器 1 核 2G , 9元/月 ,买十送二,99/年!!!快来上车!developer.huawe ...
- Kafka分区分配策略-RangeAssignor、RoundRobinAssignor、StickyAssignor
引言按照Kafka默认的消费逻辑设定,一个分区只能被同一个消费组(ConsumerGroup)内的一个消费者消费.假设目前某消费组内只有一个消费者C0,订阅了一个topic,这个topic包含7个分区 ...
- 关于“关于C#装箱的疑问”帖子的个人看法 (原发布csdn 2017年10月07日 10:21:10)
前言 昨天晚上闲着无事,就上csdn逛了一下,突然发现一个帖子很有意思,就点进去看了一下. 问题很精辟 int a = 1; object b=a; object c = b; c = 2; 为什么b ...
- vb Replace 实现
今天改一个VB程序时发现程序自带的replace 函数不知什么原因竟然不好用了 所以就自己写了一个玩玩 记录一下 'XGZ '替换字符 Private Function Replace1(ByVal ...
- vue知识点小结
vue.js中==和===的区别 1.== 用于比较.判断两者相等,比较时可自动换数据类型 2.=== 用于(严格)比较.判断两者(严格)相等,不会进行自动转换,要求进行比较的操作数必须类型一致,不一 ...
- #pragma once和#ifndef
C语言中的头文件只是简单的复制粘贴. C语言中变量.函数.结构体的定义和声明两个过程是分离的.声明通常放在头文件中,为了防止重复声明,需要保证头文件中的内容在构建obj文件时只被包含一次.这可以通过# ...
- 批量操作mysql数据库表
SELECT CONCAT('truncate TABLE ',table_schema,'.',TABLE_NAME, ';') FROM INFORMATION_SCHEMA.TABLES WHE ...
- 关于Git的用法
关于Git Git 是一个分布式版本控制软件,与CVS.Subversion一类的集中式版本控制工具不同,它采用了分布式版本库的作法,不需要服务器端软件,就可以运作版本控制,使得源代码的发布和交流极其 ...