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

    The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. 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 (<=104) and N (<=MSize) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, print the corresponding positions (index starts from 0) of the input numbers in one line. All the numbers in a line are separated by a space, and there must be no extra space at the end of the line. In case it is impossible to insert the number, print "-" instead.

    Sample Input:

    4 4
    10 6 4 15
    

    Sample Output:

    0 1 4 -

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<math.h>
     5 using namespace std;
     6 int hashTB[10001] = {0,0}, loca[10001];
     7 int isPrime(int n){
     8     int sqr = (int)sqrt(1.0 * n);
     9     if(n == 1)
    10         return 0;
    11     for(int i = 2; i <= sqr; i++){
    12         if(n % i == 0)
    13             return 0;
    14     }
    15     return 1;
    16 }
    17 int main(){
    18     int N, MSize, temp;
    19     scanf("%d%d", &MSize, &N);
    20     if(isPrime(MSize) == 0){
    21         for(int i = MSize + 1; ; i++){
    22             if(isPrime(i)){
    23                 MSize = i;
    24                 break;
    25             }
    26         }
    27     }
    28     for(int i = 0; i < N; i++){
    29         int find = 0;
    30         scanf("%d", &temp);
    31         if(hashTB[temp % MSize] == 0){
    32             hashTB[temp % MSize] = temp;
    33             loca[i] = temp % MSize;
    34         }
    35         else{
    36             for(int j = 1; j <= MSize; j++){
    37                 if(hashTB[(temp + j * j) % MSize] == 0){
    38                     hashTB[(temp + j * j) % MSize] = temp;
    39                     loca[i] = (temp + j * j) % MSize;
    40                     find = 1;
    41                     break;
    42                 }    
    43             }
    44             if(find == 0)
    45                 loca[i] = -1;
    46         }
    47     }
    48     if(loca[0] == -1)
    49         printf("-");
    50     else printf("%d", loca[0]);
    51     for(int i = 1; i < N; i++){
    52         if(loca[i] == -1)
    53             printf(" -");
    54         else printf(" %d", loca[i]);
    55     }
    56     cin >> N;
    57     return 0;
    58 }
    View Code

    总结:

    1、Quadratic probing:平方探测法。 当出现碰撞时,采用 (pos + i^2) mod size的方式探测,pos是已经哈希后的结果,而不是key。i的探测范围从1到Msize。

    2、判断素数时,不要忽略1不是素数。

  • 相关阅读:
    ES6 新特性
    基于.NET平台常用的框架整理
    你可能不知道的一些JavaScript 奇技淫巧
    js中apply方法的使用
    数据库中字段类型对应C#中的数据类型
    C# Socket SSL通讯笔记
    Media Types
    JS使用模板快速填充HTML控件数据 --- 自己写组件(0)
    FastDFS的配置、部署与API使用解读(8)FastDFS多种文件上传接口详解
    FastDFS的配置、部署与API使用解读(7)Nginx的FastDFS模块
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8519994.html
Copyright © 2011-2022 走看看