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
基本思路
可以使用拓扑排序来解这道题。基本思路如下:将输入保存在散列表后,遍历每个元素,如果元素刚好在它对应余数的位置上,则入度为0,可直接输出;否则,从余数位置出发,用线性探测法到达该位置,对于经过的所有的非空元素位置,生成一条到该元素位置的边,并将该位置入度加1;拓扑排序时,可以采用优先队列,优先输出数值较小的元素。
代码
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
#include <functional>
using namespace std;
#define MAXV 1000
vector<int> G[MAXV]; //临接表
int N,inDegree[MAXV]={0},ve[MAXV]={0}; //顶点数,边数,入度
int table[1000];
struct cmp{
bool operator () (int a,int b)
{return table[a]>table[b];}
};
int topoSort()
{
int num=0; //入队次数
priority_queue<int,vector<int>,cmp > q;
for(int i=0;i<N;i++)
{
if(inDegree[i]==0&&table[i]>=0)
q.push(i); //将度为0的结点入队
}
while(!q.empty())
{
int u=q.top(); //取出队首结点
if(num==0)
cout<<table[u];
else
cout<<' '<<table[u];
q.pop();
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
inDegree[v]--; //入度减1
if(inDegree[v]==0)
q.push(v); //入队
}
G[u].clear(); //清边,非必需
num++;
}
if(num == N)
return 1;
else
return 0;
}
int main()
{
cin>>N;
for(int i=0;i<N;i++)
{
scanf("%d",&table[i]);
}
//建邻接表并计算入度
for(int i=0;i<N;i++)
{
int pos=table[i]%N;
if(pos==i||table[i]<0)
continue;
else
{
int k=1;
int posN=(pos+k)%N;
inDegree[i]++;
G[pos].push_back(i);
while(posN!=i)
{
if(table[i]<0)
{
}
else
{
inDegree[i]++;
G[posN].push_back(i);
}
k++;
posN=(pos+k)%N;
}
}
}
topoSort();
return 0;
}
总结
不要忘记线性探测法中的取余运算,写完while循环要检查下里面关键元素的初始值和结束值到底和预期的是否一致。
11-散列4 Hashing - Hard Version的更多相关文章
- 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 (30 分)
Given a hash table of size N, we can define a hash function H(x)=x%N. Suppose that the linear probin ...
- 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 ...
- 纯数据结构Java实现(11/11)(散列)
欢迎访问我的自建博客: CH-YK Blog.
- 散列(Hash)表入门
一.概述 以 Key-Value 的形式进行数据存取的映射(map)结构 简单理解:用最基本的向量(数组)作为底层物理存储结构,通过适当的散列函数在词条的关键码与向量单元的秩(下标)之间建立映射关系 ...
- PAT A1145 Hashing - Average Search Time (25 分)——hash 散列的平方探查法
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- Algorithms - Data Structure - Perfect Hashing - 完全散列
相关概念 散列表 hashtable 是一种实现字典操作的有效数据结构. 在散列表中,不是直接把关键字作为数组的下标,而是根据关键字计算出相应的下标. 散列函数 hashfunction'h' 除法散 ...
- Hashing散列注意事项
Hashing散列注意事项 Numba支持内置功能hash(),只需__hash__()在提供的参数上调用成员函数即可 .这使得添加对新类型的哈希支持变得微不足道,这是因为扩展APIoverload_ ...
随机推荐
- debian change system language
1. select locales: 2. set language: sudo localectl set-locale LANG=zh_CN.utf8 sudo localectl set-loc ...
- shell script测试命令(test)
shell script测试命令(test) test命令 检查系统上面某些文件或者相关的属性 常用选项 test -e :检查该文件名是否存在 例:检查/dmtsai是否存在 [root@local ...
- 【转】嵌入式C语言调试开关
在调试程序时,经常会用到assert和printf之类的函数,我最近做的这个工程里就有几百个assert,在你自认为程序已经没有bug的时候,就要除去这些调试代码,应为系统在正常运行时这些用于调试的信 ...
- 【NOIP2016 Day1 T2】天天爱跑步
题目传送门:https://www.luogu.org/problemnew/show/P1600 感觉这两天在处理边界问题上有点神志不清......为了从80的暴力变成100,花了整整一个下午+一个 ...
- daterangepicker 使用方法以及各种小bug修复
双日历时间段选择插件 — daterangepicker是bootstrap框架后期的一个时间控件,可以设定多个时间段选项,也可以自定义时间段,由用户自己选择起始时间和终止时间,时间段的最大跨度可以在 ...
- VUE 源码学习01 源码入口
VUE[version:2.4.1] Vue项目做了不少,最近在学习设计模式与Vue源码,记录一下自己的脚印!共勉!注:此处源码学习方式为先了解其大模块,从宏观再去到微观学习,以免一开始就研究细节然后 ...
- datagrid参数queryParams--easyUI
datagrid参数queryParams--easyUI Html <div region="center" border="false&qu ...
- iOS 之 UITextField
UITextField 相关细节处理: 1. 设置leftView , rightView let leftView = UIView() // 设置leftView/rightView之后,勿忘设 ...
- RestServer 2.0 正式版发布
RestServer 2.0 正式版发布 使用许可&版权说明 在保持本软件完整的情况下可以将本软件用于任何商业用途. 本软件可以自由传播,但是请保持软件相关文件和说明文档完整. 未经许可不得将 ...
- Leetcode题解(十六)
44 ----------------------------------------------------------------分割线------------------------------ ...