zoukankan      html  css  js  c++  java
  • 1145. Hashing

    The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of integer keys from the table and output the average search time (the number of comparisons made to find whether or not the key is in the table). The hash function is defined to be "H(key) = key % TSize" where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.

    Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains two positive numbers: MSize, N, and M, which are the user-defined table size, the number of input numbers, and the number of keys to be found, respectively. All the three numbers are no more than 104. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space and are no more than 105.

    Output Specification:

    For each test case, in case it is impossible to insert some number, print in a line "X cannot be inserted." where X is the input number. Finally print in a line the average search time for all the M keys, accurate up to 1 decimal place.

    Sample Input:
    4 5 4
    10 6 4 15 11
    11 4 15 2
    
    Sample Output:
    15 cannot be inserted.
    2.8
    
    平方探测法先把数据插入,能插入的计算一下找位置花的时间存入p。
    计算查找的花费时,如果p不为0,直接加,如果为0,表示表里没有(要么没插入,要么插不进去,没插入的直到找到vis标记为0就返回步数,否则返回msize + 1(循环停止的条件))。
    代码:
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <map>
    #define Max 100005
    using namespace std;
    int msize,n,m,d;
    double c = 0;
    int s[10010],vis[10010],p[100010];
    bool ispri(int t)
    {
        if(t <= 1)return 0;
        if(t == 2 || t == 3)return true;
        if(t % 6 != 1 && t % 6 != 5)return false;
        for(int i = 5;i * i <= t;i += 6)
        {
            if(t % i == 0 || t % (i + 2) == 0)return false;
        }
        return true;
    }
    int nexpri(int t)
    {
        while(!ispri(t))t ++;
        return t;
    }
    int insert_(int t)
    {
        int e = t % msize;
        for(int i = 0;i < msize;i ++)
        {
            int ee = (e + i * i) % msize;
            if(!vis[ee])
            {
                s[ee] = t;
                vis[ee] = 1;
                p[t] = i + 1;
                return 1;
            }
        }
        return 0;
    }
    int search_(int t)
    {
        int e = t % msize;
        for(int i = 0;i < msize;i ++)
        {
            int ee = (e + i * i) % msize;
            if(!vis[ee] || s[ee] == t)
            {
                return i + 1;
            }
        }
        return msize + 1;
    }
    int main()
    {
        scanf("%d%d%d",&msize,&n,&m);
        msize = nexpri(msize);
        for(int i = 0;i < n;i ++)
        {
            scanf("%d",&d);
            if(!insert_(d))printf("%d cannot be inserted.
    ",d);
        }
        for(int i = 0;i < m;i ++)
        {
            scanf("%d",&d);
            c += p[d] ? p[d] : search_(d);
        }
        printf("%.1f",c / m);
    }
    View Code
  • 相关阅读:
    快速上手php:使用PhpStrom调试php
    快速上手php:使用PhpStrom部署项目
    使用自定义tld标签简化jsp的繁琐操作
    京东购物体验杂谈
    Mysql将近两个月的记录合并为一行显示
    学习WebSocket(二):使用Spring WebSocket做一个简单聊天室
    学习WebSocket(一):Spring WebSocket的简单使用
    springMVC的@ResponseBody、@RequestBody使用需要注意的地方
    如何使用maven建一个web3.0的项目
    项目管理工具 Redmine 安装试用手记
  • 原文地址:https://www.cnblogs.com/8023spz/p/8922367.html
Copyright © 2011-2022 走看看