题目


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. Linux: 查看软件安装路径

    一.        Which 命令 Shell 的which 命令可以找出相关命令是否已经在搜索路径中. 如: [root@localhost ~]# which gcc /usr/bin/gcc ...

  2. Archlinux运行FlashTool

    首先,http://www.flashtool.net/index.php下载linux版的FlashTool,并且按照其说明在/etc/udev加入如下字段: SUBSYSTEM== »usb », ...

  3. HandlerMapping 和 HandlerAdapter

    HandlerMapping 负责根据request请求找到对应的Handler处理器及Interceptor拦截器,将它们封装在HandlerExecutionChain 对象中给前端控制器返回. ...

  4. Redis 学习笔记-应用场景

    Redis作缓存系统 Redis可以对每个键设置生存时间 可以限定数据占用的最大内存空间,在数据达到空间限制后可以按照一定规则自动淘汰不需要的键. 设置方法: 修改配置文件的maxmemory参数,限 ...

  5. JavaScript面向对象深入理解原型

    原型模式 function Person(){ } Person.prototype.name="Ewarm"; Person.prototype.age="29&quo ...

  6. 重温C语言小感

    这周对我感触比较大的就是重温了下C语言,当然重点还是放到了指针那块,一看到指针就想到了链表,还有那个 指针申明,“函数指针”,“指针函数”, “使用指针实现数组降维数”,还有就是大学初学编程的点滴. ...

  7. position,display,float,overflow,margin,padding之间的相互影响

    1.元素分为块级元素和行内元素, 块级元素可以设置宽高,会自动换行,并且会发生相邻margin的合并问题.行内元素设置宽和高无效,以水平方向排列,(行内元素,绝对定位,浮动元素不会发生外边距合并)并且 ...

  8. panic: interface conversion: interface {} is nil, not chan *sarama.ProducerError

    使用golang kafka sarama 包时,遇到如下问题: 高并发情况下使用同步sync producer,偶尔遇到crash: panic: interface conversion: int ...

  9. scp命令,用来在本地和远程相互传递文件,非常方便

    scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的.可能会稍微影响一下速度.当你服务器 ...

  10. Bridging signals(NlogN最长上升子序列)

    Bridging signals Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...