zoukankan      html  css  js  c++  java
  • pat1078. Hashing (25)

    1078. Hashing (25)

    时间限制
    100 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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<stack>
     3 #include<algorithm>
     4 #include<iostream>
     5 #include<stack>
     6 #include<set>
     7 #include<map>
     8 #include<cmath>
     9 using namespace std;
    10 void getprime(int &msize){
    11     int i=msize,j;
    12     if(msize==0||msize==1||msize==2){
    13         msize=2;
    14         return;
    15     }
    16     for(;;i++){
    17         if(i%2==0){
    18             continue;
    19         }
    20         int maxl=sqrt(msize*1.0);
    21         for(j=3;j<=maxl;j+=2){
    22             if(i%j==0){
    23                 break;
    24             }
    25         }
    26         if(j>maxl){
    27             msize=i;
    28             return;
    29         }
    30     }
    31 }
    32 map<int,int> ha;
    33 bool g[10005];
    34 int main(){
    35     //freopen("D:\INPUT.txt","r",stdin);
    36     int i,msize,n,num;
    37     scanf("%d %d",&msize,&n);
    38     getprime(msize);
    39 
    40     //cout<<msize<<endl;
    41 
    42     for(i=0;i<n;i++){
    43         scanf("%d",&num);
    44         ha[i]=num%msize;
    45     }
    46     int half=msize/2;
    47     map<int,int>::iterator it=ha.begin();
    48     g[it->second]=true;
    49     printf("%d",it->second);
    50     it++;
    51     for(;it!=ha.end();it++){
    52         if(!g[it->second]){
    53             g[it->second]=true;
    54             printf(" %d",it->second);
    55         }
    56         else{
    57             for(i=1;i<=half;i++){
    58                 if(!g[(it->second+i*i)%msize]){
    59                     g[(it->second+i*i)%msize]=true;
    60                     printf(" %d",(it->second+i*i)%msize);
    61                     break;
    62                 }
    63             }
    64             if(i>half){
    65                 printf(" -");
    66             }
    67         }
    68     }
    69     printf("
    ");
    70     return 0;
    71 }
  • 相关阅读:
    MySQL事件(定时任务)
    MySQL存储过程
    WebSocket小记
    Python计算给定日期位于当年第几周
    报错解决——Failed building wheel for kenlm
    计算机基础系列之压缩算法
    计算机基础系列之内存、磁盘、二进制
    计算机基础系列之CPU
    常用正则表达大全
    报错解决——TypeError: LoadLibrary() argument 1 must be str, not None
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4783066.html
Copyright © 2011-2022 走看看