PTA 11-散列2 Hashing(详解)

11-散列2 Hashing(25 分)

题目地址:11-散列2 Hashing(25 分)

题目描述:

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 H ( k e y ) = k e y where TSize T S i z e 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.


  • 输入格式
    Each input file contains one test case. For each case, the first line contains two positive numbers: MSize(104) M S i z e ( ≤ 10 4 ) and N(MSize) N ( ≤ M S i z e ) 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.

  • 输出格式
    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.


解题方法:
这道题目只需要用一个数组就可以解决,开始对数组每个元素先初始化为0,然后通过散列映射到数组中去,如果该映射的下标的元素值为0,则把该下标置为我们输入的值,否则就遍历正向的二次探测。这里大家可能会对什么时候才能结束探测循环表示疑问。事实上,当增量为 TableSize2 T a b l e S i z e 2 时,可以把 TableSize2TableSize T a b l e S i z e 2 对 T a b l e S i z e 取余,发现为0,然后再看 (TableSize+1)2 ( T a b l e S i z e + 1 ) 2 TableSize T a b l e S i z e 取余,发现为1。继续下去可以发现,当超过TableSize时,如 TableSize+a T a b l e S i z e + a 取余结果就是 a a ,因此我们循环只需要到TableSize就可以结束。


易错点:
对1来说,他不是素数(质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数),所以对他而言最小素数为2




程序:

#include 
#include 
#include 

int GetNextPrime(int x)
{   /* 若为偶数,找当前数下一个最小的素数 */
    if (x == 1)      // 1不是素数 
        return 2;
    int i, p = x % 2 == 1 ? x : x+1;
    while (1)
    {
        for (i = sqrt(p); i >= 2; i--)
            if (p % i == 0)
                break;
        if (i == 1)
            break;
        else
            p += 2;
    }   
    return p;
}

int Hash(int key, int TableSize)
{   /* 获取映射 */
    return key % TableSize;
}

int main(int argc, char const *argv[])
{
    int TableSize, N, x, pos, tempPos;
    scanf("%d %d", &TableSize, &N);
    TableSize = GetNextPrime(TableSize);
    int A[TableSize];
    for (int i = 0; i < TableSize; i++)
        A[i] = 0;
    for (int i = 0; i < N; i++)
    {
        if (i != 0)     /* 规格化输出格式 */
            printf(" ");
        scanf("%d", &x);
        pos = Hash(x, TableSize);
        tempPos = pos;
        if (A[tempPos] == 0)
        {   /* 如果当前下标未被使用 */
            A[tempPos] = x;
            printf("%d", pos);
        }
        else
        {
            int cnt, flag = 0;
            for (cnt = 1; cnt < TableSize; cnt++)
            {
                pos = Hash(tempPos + cnt*cnt, TableSize);
                if (A[pos] == 0)
                {   /* 如果找到不冲突的点 */
                    flag = 1;
                    A[pos] = x;
                    printf("%d", pos);
                    break;
                }
            }
            if (flag == 0)
                printf("-");
        }
    }
    return 0;
}

如果对您有帮助,帮忙点个小拇指呗~

你可能感兴趣的:(PTA)