Hashing - Hard Version
Hashing - Hard Version
Given a hash table of size N, we can define a hash function . 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
分析
参考Messier
可以使用拓扑排序来解这道题。基本思路如下:将输入保存在散列表后,遍历每个元素,如果元素刚好在它对应余数的位置上,则入度为0,可直接输出;否则,从余数位置出发,用线性探测法到达该位置,对于经过的所有的非空元素位置,生成一条到该元素位置的边,并将该位置入度加1;拓扑排序时,可以采用优先队列,优先输出数值较小的元素
代码如下
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
vector<int> G[1000]; // 连接表
int N;
int indegree[1000]={0}; // 入度
int a[1000];
struct mycompare{ // 为优先队列自定义操作
bool operator()(int m,int n){
return a[m]>a[n];
}
};
void toposort(){ // 拓扑排序
int tag=0;
priority_queue<int,vector<int>,mycompare> q;
for(int i=0;i<N;i++)
if(indegree[i]==0&&a[i]>=0)
q.push(i);
while(!q.empty()){
int t=q.top();
if(tag++==0) cout<<a[t];
else cout<<" "<<a[t];
q.pop();
for(int i=0;i<G[t].size();i++){
int v=G[t][i];
indegree[v]--;
if(indegree[v]==0)
q.push(v);
}
}
}
int main(){
cin>>N;
for(int i=0;i<N;i++)
cin>>a[i];
for(int i=0;i<N;i++){ // 建图
if(a[i]%N==i||a[i]<0)
continue;
else
{
int t=a[i]%N;
while(t!=i){
G[t].push_back(i);
indegree[i]++;
t=(t+1)%N;
}
}
}
toposort();
return 0;
}
Hashing - Hard Version的更多相关文章
- 7-18 Hashing - Hard Version
7-18 Hashing - Hard Version (30 分) Given a hash table of size N, we can define a hash function . Sup ...
- pat09-散列3. Hashing - Hard Version (30)
09-散列3. Hashing - Hard Version (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 HE, Qin ...
- 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 基本思路 ...
- 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 (30 分)
Given a hash table of size N, we can define a hash function H(x)=x%N. Suppose that the linear probin ...
- PTA 11-散列4 Hard Version (30分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/680 5-18 Hashing - Hard Version (30分) Given ...
- 中国大学MOOC-陈越、何钦铭-数据结构-2017春
中国大学MOOC-陈越.何钦铭-数据结构-2017春 学习地址 详细学习内容 Github记录地址 欢迎fork和star,有惊喜值得学习! 参考学习笔记 参考AC代码 数据结构和算法学习笔记 学习内 ...
- A Universally Unique IDentifier (UUID) URN Namespace
w Network Working Group P. Leach Request for Comments: 4122 Microsoft Category: Standards Track M. M ...
- ASP.NET Core搭建多层网站架构【2-公共基础库】
2020/01/28, ASP.NET Core 3.1, VS2019,Newtonsoft.Json 12.0.3, Microsoft.AspNetCore.Cryptography.KeyDe ...
随机推荐
- 深入理解JMM(Java内存模型) --(五)锁
锁的释放-获取建立的happens before 关系 锁是Java并发编程中最重要的同步机制.锁除了让临界区互斥执行外,还可以让释放锁的线程向获取同一个锁的线程发送消息. 下面是锁释放-获取的示例代 ...
- CMake使用总结
总结CMake的常用命令,并介绍有用的CMake资源. CMake意为cross-platform make,可用于管理c/c++工程.CMake解析配置文件CMakeLists.txt生成Makef ...
- 【161】BASH相关文章链接
---恢复内容开始--- 1. Linux cat命令详解 --<cat>-- 新建文件 file1.txt,随便输入几行文字 cat 'file1.txt' #显示 'file1.tx ...
- WPF-CheckBox(复选框、功能开关)美化
老规矩,先放图 按钮美化背景: 由于特殊需求,复选框样式单一,所以我们需要将其按钮重构和美化达到我们的需求 复选框美化思维引导: 图中1为背景色 图中2为边框 图中3为句柄控件组成(Path+Rect ...
- gdb如何保存和读取断点
刚开始在linux下学编程使用gdb的同学可能会发现,每次用gdb设置断点调试程序,但下次打开的时候所有断点都没有了,很不方便.下面介绍保存和读取断点的方法. 1. 保存断点 先用info b 查看一 ...
- 清除浮动(float)的影响
浮动会导致父元素塌陷如图: 解决办法: 父元素overflow:hidden,如图 末尾插入子元素clear,如图 为甚么,父元素overflow:hidden会解决塌陷问题? 来自知乎貘吃馍香的回答 ...
- Android基础TOP6_2:Gallery +Image完成画廊
Activity: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmln ...
- 可滚动的ResultSet类型 实现分页
可滚动的ResultSet类型. 这个类型支持前后滚动取得纪录next().previous(),回到第一行first(),同时还支持要取的 ResultSet中的第几行 absolute(int n ...
- socket相关函数
socket() 我们使用系统调用socket()来获得文件描述符:#include<sys/types.h>#include<sys/socket.h>int socket( ...
- Hibernate框架之HQL查询与Criteria 查询的区别
Hibernate框架提供了HQL查询和Criteria 查询.下面对这两种查询分别做个例子.也好对这两种查询方法有个大概的了解.就用房屋信息表做例子,查询所有房屋信息. HQL语句查询所有房屋信息: ...