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_ ...
随机推荐
- 【NOIP2015提高组】Day2 T2 子串
题目描述 有两个仅包含小写英文字母的字符串 A 和 B.现在要从字符串 A 中取出 k 个互不重叠的非空子串,然后把这 k 个子串按照其在字符串 A 中出现的顺序依次连接起来得到一 个新的字符串,请问 ...
- [ACdream] 女神教你字符串——三个气球
Problem Description 女神邀请众ACdream开联欢会,显然作为ACM的佼佼者,气球是不能少的~.女神准备了三种颜色的气球,红色,黄色,绿色(交通信号灯?) 有气球还不能满足女神,女 ...
- JS的简单用法
JS的简单用法 参考:http://www.w3school.com.cn/js/js_switch.asp JavaScript 是网络的脚本语言 JavaScript 是可插入 HTML 页面的编 ...
- LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others ...
- LeetCode 219. Contains Duplicate II (包含重复项之二)
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- 面试中常用排序算法实现(Java)
当我们进行数据处理的时候,往往需要对数据进行查找操作,一个有序的数据集往往能够在高效的查找算法下快速得到结果.所以排序的效率就会显的十分重要,本篇我们将着重的介绍几个常见的排序算法,涉及如下内容: 排 ...
- 通过 ODBC 访问数据库获取数据集
Step1:(window 中完成): 控制面板/管理工具/ODBC 数据源/用户 Step2:(window 中完成): 添加/SQL Server Step3:(window 中完成): 自己定义 ...
- eclipse禁用svg文件Validation
1.打开window>preferences>validation找到xml validator 2.点击xml validator最右侧的按钮打开xml校验规则窗口,选中exclude ...
- Hive安装和部署
在root的用户下搭建的 构建hive之前必须要先搭建好hadoop才可以. hive定义了一种类似SQL查询语言--HQL 类似SQL ,但是不完全相同 Hive是一个数据仓库,它部署在Hadoop ...
- Maven中settings.xml的配置项说明精讲
1.Maven的配置文件(Maven的安装目录/conf/settings.xml ) 和 Maven仓库下(默认的Maven仓库的是用户家目录下的.m2文件,可以另行制定)的settings.xml ...