题目


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的更多相关文章

  1. pat09-散列3. Hashing - Hard Version (30)

    09-散列3. Hashing - Hard Version (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 HE, Qin ...

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

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

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

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

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

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

  6. 散列(Hash)表入门

    一.概述 以 Key-Value 的形式进行数据存取的映射(map)结构 简单理解:用最基本的向量(数组)作为底层物理存储结构,通过适当的散列函数在词条的关键码与向量单元的秩(下标)之间建立映射关系 ...

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

  8. Algorithms - Data Structure - Perfect Hashing - 完全散列

    相关概念 散列表 hashtable 是一种实现字典操作的有效数据结构. 在散列表中,不是直接把关键字作为数组的下标,而是根据关键字计算出相应的下标. 散列函数 hashfunction'h' 除法散 ...

  9. Hashing散列注意事项

    Hashing散列注意事项 Numba支持内置功能hash(),只需__hash__()在提供的参数上调用成员函数即可 .这使得添加对新类型的哈希支持变得微不足道,这是因为扩展APIoverload_ ...

随机推荐

  1. 聊聊Vue.js组件间通信的几种姿势

    写在前面 因为对Vue.js很感兴趣,而且平时工作的技术栈也是Vue.js,这几个月花了些时间研究学习了一下Vue.js源码,并做了总结与输出. 文章的原地址:https://github.com/a ...

  2. C# post提交

    WebForm 前台 <asp:Button ID="Button1" runat="server" Text="Button" On ...

  3. C#通过OpenCL调用显卡GPU做高效并行运算

    GPU的并行运算能力远超CPU,有时候我们会需要用到超大数据并行运算,可以考虑用GPU实现,这是一篇C#调用GPU进行运算的入门教程. 1: 下载相关的库: https://sourceforge.n ...

  4. 实现一个单隐层神经网络python

    看过首席科学家NG的深度学习公开课很久了,一直没有时间做课后编程题,做完想把思路总结下来,仅仅记录编程主线. 一 引用工具包 import numpy as np import matplotlib. ...

  5. LeetCode 485. Max Consecutive Ones (最长连续1)

    Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...

  6. HandlerThread学习

    之前基本讲过Handler的一些知识了,我们今天学习下Google封装的一个实现线程通信的一个类HandlerThread 一.HandlerThread使用 @Override protected ...

  7. 不定期更新的CSS样式设置

    头像图片的样式 假设这是一个头像图片,假设展示头像的框为100*100的div,而图片尺寸为510*765,如何让图片显示成这样呢? html结构很简单: <div class="im ...

  8. 轻松学会ES6新特性之生成器

    生成器虽然是ES6最具魔性的新特性,但也是最难懂得的一节,笔者写了大量的实例来具体化这种抽象的概念,能够让人一看就懂,目的是希望别人不要重复或者减少笔者学习生成器的痛苦经历. 在说具体的ES6生成器之 ...

  9. TIDB 参数解释

    地址:https://github.com/pingcap/docs-cn/blob/master/op-guide/configuration.md#tidb TiDB --store 用来指定 T ...

  10. 译:Asp.Net Identity与Owin,到底谁是谁?

    送给正在学习Asp.Net Identity的你 :-) 原文出自 trailmax 的博客AspNet Identity and Owin. Who is who. Recently I have ...