题目地址

https://pta.patest.cn/pta/test/16/exam/4/question/680

5-18 Hashing - Hard Version   (30分)

Given a hash table of size NN, 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 NN (\le 1000≤1000), which is the size of the hash table. The next line contains NN 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

根据hash表算原始的插入顺序,很有意思的题,mooc讲了思路。
/*
评测结果
时间 结果 得分 题目 编译器 用时(ms) 内存(MB) 用户
2017-07-08 11:46 答案正确 30 5-18 gcc 6 4
测试点结果
测试点 结果 得分/满分 用时(ms) 内存(MB)
测试点1 答案正确 18/18 2 1
测试点2 答案正确 4/4 2 1
测试点3 答案正确 2/2 2 1
测试点4 答案正确 2/2 2 1
测试点5 答案正确 4/4 6 4 刚开始找应该输出的点时,暴力扫描取最小,结果发现第三个点超时
后来改用堆找最小结点
发现还是超时。。。
然后没辙了,找了个带注释的测试点截图,第三个点写到,有非-1的空位 WTF! 改了之后发现这个数据量,用暴力扫最小结点耗时也是十几毫秒,没慢多少,但是按理说还是用堆比较科学。
————————————————
读入hash表-> 表里i位置上的元素,如果算完hash应该插在j位置上,那么从i-1,一直减到j,这些元素都要在i之前插入。
用(i-1+N)%N解决hash表的循环问题 把依赖关系计算完后,此时问题就成了拓扑排序。
再接下来就是怎么找入度为0的结点中,包含元素最小的点了。
此解法建了堆,最开始把入度为0的全部提前压进去,然后开始循环往外取。
取出后断开该结点发出的边与其它点的连接。如果断开后发现其它结点入度变成了0,那么立即把该入度为0的点扔到堆里。
*/
#include<stdio.h>
#define TRUE 1
#define FALSE 0
#define MAXN 1000
#define EMPTY -1
#define INFINITY 6666666 struct HashNode
{
int value;
int visited; //是否访问过
int noCollision; //是否有冲突,建矩阵的时候用
int indegree; //入度
} gHashTable[MAXN]; int gMatrix[MAXN][MAXN];
int Heap[MAXN+1]; //堆
int HeapLength=0; //堆长 void DBG_printHeap() //debug时打印堆的
{
int i;
for(i=1;i<=HeapLength;i++)
{
printf("%d ",gHashTable[Heap[i]].value);
}
printf("+++++\n");
}
void InsertIntoHeap(int a) //插入堆
{
int i;
Heap[++HeapLength]=a;
i=HeapLength;
while(i/2>0)
{
if(gHashTable[Heap[i/2]].value>gHashTable[a].value)
{
Heap[i]=Heap[i/2];
i=i/2;
}
else break;
}
Heap[i]=a;
// DBG_printHeap();
} int DeHeap() //弹出堆顶
{
if(HeapLength==0)
return EMPTY;
int top=Heap[1];
int temp,parent,child;
Heap[1]=Heap[HeapLength--];
temp=Heap[1];
for(parent=1;2*parent<=HeapLength;parent=child)
{
child=parent*2;
if(child!=HeapLength)
{
if(gHashTable[Heap[child]].value>gHashTable[Heap[child+1]].value)
child++;
} if(gHashTable[temp].value>gHashTable[Heap[child]].value)
Heap[parent]=Heap[child];
else break;
}
Heap[parent]=temp;
// DBG_printHeap();
return top;
} void CalcMatrix(int N) //计算依赖关系的邻接矩阵
{
int i,idx,p;
for(i=0;i<N;i++)
{
if(gHashTable[i].noCollision==1 || gHashTable[i].value==EMPTY)
continue;
idx=gHashTable[i].value%N;
p=(i-1+N)%N;
while(p!=idx)
{
if(gHashTable[p].value!=EMPTY)
gMatrix[p][i]=TRUE;
gHashTable[i].indegree++;
p=(p-1+N)%N;
}
gMatrix[idx][i]=TRUE;
gHashTable[i].indegree++;
}
} void DeleteEdges(int idx,int N) //删边
{
int i;
for(i=0;i<N;i++)
{
if(gMatrix[idx][i])
{
// gMatrix[idx][i]=FALSE;
gHashTable[i].indegree--;
if(gHashTable[i].indegree==0)
{
// gHashTable[i].visited=TRUE;
InsertIntoHeap(i);
}
}
}
// gHashTable[idx].visited=TRUE;
} int InitHeapBeforePop(int N) //最开始时扫描结点,把入度为零的扔堆里面
{
int i; for(i=0;i<N;i++)
{
if(gHashTable[i].visited==TRUE || gHashTable[i].indegree!=0)
continue;
else
InsertIntoHeap(i);
}
} int main()
{
int N,i,temp,firstprint;
scanf("%d",&N);
for(i=0;i<N;i++)
{
scanf("%d",&gHashTable[i].value);
if(gHashTable[i].value%N==i)
gHashTable[i].noCollision=TRUE;// 下标和hash值相符,说明没冲突
if(gHashTable[i].value<0)
gHashTable[i].value=EMPTY; //标识空位
if(gHashTable[i].value==EMPTY)
gHashTable[i].visited=TRUE; //把空位的visited置1,后续不处理
}
CalcMatrix(N);
InitHeapBeforePop(N);
firstprint=1; //处理格式问题,第一个字符前面不加空格
while((temp=DeHeap()) != EMPTY)
{
DeleteEdges(temp,N);
if(!firstprint)
putchar(' ');
printf("%d",gHashTable[temp].value);
firstprint=0;
}
}

  

PTA 11-散列4 Hard Version (30分)的更多相关文章

  1. 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 ...

  2. PTA 7-1 是否完全二叉搜索树 (30分)

    PTA 7-1 是否完全二叉搜索树 (30分) 将一系列给定数字顺序插入一个初始为空的二叉搜索树(定义为左子树键值大,右子树键值小),你需要判断最后的树是否一棵完全二叉树,并且给出其层序遍历的结果. ...

  3. PTA二叉搜索树的操作集 (30分)

    PTA二叉搜索树的操作集 (30分) 本题要求实现给定二叉搜索树的5种常用操作. 函数接口定义: BinTree Insert( BinTree BST, ElementType X ); BinTr ...

  4. PTA 逆散列问题 (30 分)(贪心)

    题目链接:https://pintia.cn/problem-sets/1107178288721649664/problems/1107178432099737614 题目大意: 给定长度为 N 的 ...

  5. 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 ...

  6. JavaScript数据结构-11.散列

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. PTA 最大子列和问题(10 分)

    最大子列和问题(10 分) 给定K个整数组成的序列{ N​1​​, N​2​​, ..., N​K​​ },“连续子列”被定义为{ N​i​​, N​i+1​​, ..., N​j​​ },其中 1≤ ...

  8. 纯数据结构Java实现(11/11)(散列)

    欢迎访问我的自建博客: CH-YK Blog.

  9. 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 ...

随机推荐

  1. 洛谷 P3387 【模板】缩点

    题目背景 缩点+DP 题目描述 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只 ...

  2. MovieReview—Ghost in the Shell 2: Innocence(攻壳机动队2:无罪)

    Doll killing event            The movie was developed around a series of doll murders. Barthes and o ...

  3. UVA 177 PaperFolding 折纸痕 (分形,递归)

    著名的折纸问题:给你一张很大的纸,对折以后再对折,再对折……每次对折都是从右往左折,因此在折了很多次以后,原先的大纸会变成一个窄窄的纸条.现在把这个纸条沿着折纸的痕迹打开,每次都只打开“一半”,即把每 ...

  4. Lemonade Trade

    4990: Lemonade Trade 时间限制: 1 Sec  内存限制: 128 MB  Special Judge提交: 88  解决: 17[提交][状态][讨论版][命题人:admin] ...

  5. 剑指offer55 字符流中第一个不重复的字符(最典型错误)

    典型并且基础的错误: class Solution { public: //Insert one char from stringstream void Insert(char ch) { ) res ...

  6. appium---命令行启动appium

    在客户端的appium长时间运行的时候,出产生一些数据.日志有可能会对appium的内存有所增长,严重的会使appium产生崩溃,这个时候就推荐使用通过cmd进行运行appium, 安装前提需要安装N ...

  7. 获取kafka的lag, offset, logsize的shell和python脚本

    python脚本 #!/usr/bin/env python import os import re import sys group_id=sys.argv[1] pn=sys.argv[2] ka ...

  8. Python——print用法详解

    1.print语法格式 print()函数具有丰富的功能,详细语法格式如下:print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=Fa ...

  9. spring mvc + freemarker 整合

    <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.s ...

  10. 汉明码(Hamming Code)原理及实现

    汉明码实现原理 汉明码(Hamming Code)是广泛用于内存和磁盘纠错的编码.汉明码不仅可以用来检测转移数据时发生的错误,还可以用来修正错误.(要注意的是,汉明码只能发现和修正一位错误,对于两位或 ...