5-17 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 \% TSizeH(key)=key%TSizewhere TSizeTSize 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: MSizeMSize(\le 10^4≤10?4??) and NN (\le MSize≤MSize) which are the user-defined table size and the number of input numbers, respectively. Then NN 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 -
/*
 * 这题就是除留余数法插入 平方探测法解决冲突(这里只有正向) 实现散列表。
*/
#include "iostream"
#include "cmath"
using namespace std;
#define MAXSIZE 20000 /* 带进去算了下这区间有素数 10006 我也是试着取得0..0.。*/
int nextPrime(int n) {
    int i, j;
    bool flag = true;
    if (n % 2 == 0)
        n++;
    if (n == 1) /* 判断素数要注意1啊~ 在这里卡了2次- - */
        return 2;
    for (i = n; i < MAXSIZE; i+=2) {
        int k = sqrt(i);
        for (j = 2; j<=k ; j++)
            if (!(i%j))
                break;
        if (j > k)
            return i;
    }
    return i;
}

int val[10001];
void find(int a[],int m,int n) {
    int i, j;
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            int pos = (val[i]%n + j*j) % n;
            if (a[pos] == val[i])
            {
                if (i == 0)
                    cout << pos;
                else
                    cout << " " << pos;
                break;
            }
        }
        if (j == n)
            if (i == 0) {
                cout << "-";
            }
            else {
                cout << " " << "-";
            }
    }
}

void insert(int n,int m,int a[]) {
    for (int i = 0; i < m; i++) {
        cin >> val[i];
        for (int j = 0; j < n; j++) {
            int pos = (val[i]%n + j*j) % n;
            if (!a[pos]) {
                a[pos] = val[i];
                break;
            }
        }
    }
}
int main() {
    int n, m;
    int a[20001];
    cin >> n >> m;
    n = nextPrime(n);
    for (int i = 0; i < n; i++)
        a[i] = 0;
    insert(n, m, a); /* 映射 */
    find(a, m, n);
    cout << endl;
    return 0;
}
时间: 2024-10-06 10:48:09

5-17 Hashing (25分)的相关文章

4-9 二叉树的遍历 (25分)

4-9 二叉树的遍历   (25分) 输出样例(对于图中给出的树): Inorder: D B E F A G H C I Preorder: A B D F E C G H I Postorder: D E F B H G I C A Levelorder: A B C D F G I E H 代码:(都是遍历的算法) 1 // 4-9 二叉树的遍历 2 // 3 // Created by Haoyu Guo on 04/02/2017. 4 // Copyright ? 2017 Haoy

5-24 树种统计 (25分)

5-24 树种统计   (25分) 随着卫星成像技术的应用,自然资源研究机构可以识别每一棵树的种类.请编写程序帮助研究人员统计每种树的数量,计算每种树占总数的百分比. 输入格式: 输入首先给出正整数N(\le 10^5≤10?5??),随后N行,每行给出卫星观测到的一棵树的种类名称.种类名称由不超过30个英文字母和空格组成(大小写不区分). 输出格式: 按字典序递增输出各种树的种类名称及其所占总数的百分比,其间以空格分隔,保留小数点后4位. 输入样例: 29 Red Alder Ash Aspe

5-20 表达式转换 (25分)

5-20 表达式转换 (25分) 算术表达式有前缀表示法.中缀表示法和后缀表示法等形式.日常使用的算术表达式是采用中缀表示法,即二元运算符位于两个运算数中间.请设计程序将中缀表达式转换为后缀表达式. 输入格式: 输入在一行中给出不含空格的中缀表达式,可包含+.-.*.\以及左右括号( ),表达式不超过20个字符. 输出格式: 在一行中输出转换后的后缀表达式,要求不同对象(运算数.运算符号)之间以空格分隔,但结尾不得有多余空格. 输入样例: 2+3*(7-4)+8/4 输出样例: 2 3 7 4

5-3 树的同构 (25分)

5-3 树的同构   (25分) 给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右孩子互换后,就得到另外一棵树.而图2就不是同构的. 图1 图2 现给定两棵树,请你判断它们是否是同构的. 输入格式: 输入给出2棵二叉树树的信息.对于每棵树,首先在一行中给出一个非负整数NN (\le 10≤10),即该树的结点数(此时假设结点从0到N-1N?1编号):随后NN行,第i

用1分,5分,10分,25分,50分硬币凑成一元,总共有几种组合办法?(SQL 谜题)

早在ITPUB中看过有个SQL高手,喜欢出谜题,以下是一个谜题.我试用SQL SERVER解决此问题. 用1分,5分,10分,25分,50分硬币凑成一元,总共有几种组合办法? SELECT'1*'+rtrim(a.number) +'+5*'+rtrim(b.number) +'+10*'+rtrim(c.number) +'+25*'+rtrim(d.number) +'+50*'+rtrim(e.number)AS result FROM(select number from master.

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 func

PTA 10-排序5 PAT Judge (25分)

题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/677 5-15 PAT Judge   (25分) The ranklist of PAT is generated from the status list, which shows the scores of the submissions. This time you are supposed to generate the ranklist for PAT. Input Spe

PTA 09-排序1 排序 (25分)

题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/720 5-12 排序   (25分) 给定NN个(长整型范围内的)整数,要求输出从小到大排序后的结果. 本题旨在测试各种不同的排序算法在各种数据情况下的表现.各组测试数据特点如下: 数据1:只有1个元素: 数据2:11个不相同的整数,测试基本正确性: 数据3:103个随机整数: 数据4:104个随机整数: 数据5:105个随机整数: 数据6:105个顺序整数: 数据7:105个逆序整数

PAT 1078. Hashing (25)

1078. 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 % TSize" where TSize is the