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. TortoiseSVN客户端更改新的URL和账号

    一: 变更SVN地址 右键(TortoiseSVN) → Relocate → 输入你新的URL地址 二:变更账号 TortoiseSVN右键->Setting 进入“Setting”之后,也就 ...

  2. python--unittest测试框架

    unittest中最核心的四个概念是:test case, test suite, test runner, test fixture

  3. Java 8——保存参数名称

    一.详述 在很多情况下,程序需要保存方法参数名称,如Mybatis中的mapper和xml中sql的参数绑定.但是在java 8之前的编译器是不支持保存方法参数名至class文件中的. 所以很多框架都 ...

  4. alpine安装sshd/ssh server

    1.下载alpine镜像 1 2 3 4 5 6 7 8 9 10 [root@docker43 ~]# docker pull alpine Using default tag: latest Tr ...

  5. vi/vim的快捷操作(2)

    1.拷贝当前行[yy],拷贝当前行向下的5行[5yy],并粘贴[p] 2.删除当前行[dd],删除当前行向下的5行[5dd] 3.在文件中查找某个单词,命令行模式下输入[/关键字],回车查找,输入[n ...

  6. 开发工具--浅谈Git

    工具|浅谈Git Git这个工具,是我一直想写文章,终于我实现了我的想法.在我开始写之前,发表一下自己的看法,git只是一个工具,既然已经认定是一个工具,那么一定具备工具这类的共同特征,请用面向对象的 ...

  7. php+Apache2+Nginx+Mysql

        Nginx         1.安装Nginx     sudo apt-get clean     sudo apt-get update     sudo apt-get install ...

  8. cs1.6 人物地址查询

    打开游戏 控制台命令 增加一个BOT:bot_add 增加一个警察:bot_add_ct 增加一个匪徒:bot_add_t 让BOT站在原地不动:bot_stop 1 "-"减号: ...

  9. Ionic run android失败解决方法。

    今天在node.js窗口执行命令行ionic run android时就是报Error executing "adb device":error错,一开始以为是SDK不全,后来以为 ...

  10. FPM十一:点击POPUP显示明细

    沿接着前面的Search和List.在LIST中点击一列,弹出窗口显示明细. 1.list中定义事件: METHOD if_fpm_guibb_list~get_definition. DATA:gt ...