zoukankan      html  css  js  c++  java
  • 11.散列2 Hashing [二次探测(平方探测)法]

    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%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 (≤10
    ​4
    ​​ ) 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 -
    ——————————————————————————————

    #include<iostream>
    #include<cmath>
    #define maxsize 1000000
    using namespace std;
    struct node {
    	int data;
    	int flag; //状态位
    };
    struct hashtable {
    	int tablesize;
    	node* nodes;//指向数组的第一个元素
    };
    int Hash(int key, int p) {
    	return key % p;
    }
    int gethashsize(int n) {
    	if (n < 2)return 2;
    	int i;
    	int p = n % 2 ? n + 2 : n + 1;
    	while (p <= maxsize) {
    		for (i = (int)sqrt(p); i > 2; i--)
    			if (!(p % i)) break;
    		if (i == 2) break;
    		else p += 2;
    	}
    	return p;
    }
    hashtable* creatable(int tablesize) {
    	hashtable* h = new hashtable;
    	h->tablesize = gethashsize(tablesize);
    	h->nodes = new node[h->tablesize];
    	for (int i = 0; i < h->tablesize; i++)
    		h->nodes[i].flag = -1;
    	return h;
    }
    int find(hashtable* h, int key) {
    	int newpos, currentpos;//记录新的位置和当前位置
    	int cnum = 0;//记录冲突次数
    	currentpos = newpos = Hash(key, h->tablesize);
    	while (h->nodes[newpos].flag != -1 && h->nodes[newpos].data != key) {
    		cnum++;
    		newpos = currentpos + cnum * cnum;
    		if (newpos >= h->tablesize) {
    			newpos = newpos % h->tablesize;
    			if (newpos == currentpos)
    				break;
    		}
    	}
    	return newpos;
    }
    int insert(hashtable* h, int key) {
    	int pos = find(h, key);
    	if (h->nodes[pos].flag == -1) {
    		h->nodes[pos].flag = 0;
    		h->nodes[pos].data = key;
    		return pos;
    	}
    	else
    		return -1;
    }
    void Delete(hashtable* h) {
    	if(!h)
    		delete[]h->nodes;
    }
    int main() {
    	hashtable* h;
    	int m, i, n, tmp;
    	int pos;
    	cin >> m >> n;
    	h = creatable(m);
    	for (i = 0; i < n; i++) {
    		cin >> tmp;
    		pos = insert(h, tmp);
    		if (i != 0) cout << " ";
    		if (pos == -1)
    			cout << "-";
    		else
    			cout << pos;
    	}
    	Delete(h);
    
    	return 0;
    }
    
  • 相关阅读:
    常用正则表达式
    用Python开始机器学习(2:决策树分类算法)
    Query意图分析:记一次完整的机器学习过程(scikit learn library学习笔记)
    如何成为python高手(转)
    scikit-learn——快速入门
    程序员训练机器学习 SVM算法分享
    应用scikit-learn做文本分类
    sklearn文本特征提取
    中文分词入门之字标注法4
    poj 2965 The Pilots Brothers&#39; refrigerator(dfs 枚举 +打印路径)
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13109986.html
Copyright © 2011-2022 走看看