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
  • 相关阅读:
    python中is和==的区别
    深拷贝和浅拷贝
    编码和解码
    with语句处理异常
    python中运行flask报错:UnicodeDecodeError: 'utf8' codec can't decodebyte 0xd5 in position 0:invalid continuation byte
    python中update的基本使用
    python中的程序控制结构
    python中的四种存储结构总结
    python中list,tuple,dict,set特点对比总结
    解决UIScrollview无故偏移和导航条遮挡view的问题
  • 原文地址:https://www.cnblogs.com/8023spz/p/8922367.html
Copyright © 2011-2022 走看看