首页 > 代码库 > pat解题报告【1078】

pat解题报告【1078】

1078. Hashing (25)

时间限制  
100 ms
内存限制  
32000 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 -

 

这题本质靠hash算法里面的二次试探法。h_new=(h_old+cc*cc)%sizet, cc=1,2,3,4······。h_old是原来的hash值,h_new是新得到的hash值。其实原来的二次试探法是双向的,刚开始我也按双向的做了。这个题目就坑在只要求正向增长试探,不要双向,双向会错。(with positive increments only)!!!

这个题目还有一点:要求hash表的大小是素数,如果不是素数那么取比这个数大的最小的素数。

这里涉及到素数生成算法和找对应的素数两个算法。

素数生成算法:

void init() {
	memset( vis, 0, sizeof( vis ) );
	memset( is_prime, 0, sizeof( is_prime ) );
	is_prime[0] = is_prime[1] = 1;
	int i, j;

	for ( i = 2; i < maxn; i++ ) {
		for ( j = 2; j * i < maxn; j++ )
			is_prime[i * j] = 1;
	}

   cnt = 0;

	for ( i = 2; i < maxn; i++ ) {
		if ( !is_prime[i] )
			prime[cnt++] = i;
	}
}


算素数网上有几种算法,比如说筛选法,相除法,这里用了一种比较朴素的思想,就是算出所有合数,用数组标记,那么剩下的就是素数。prime数组里面存的就是素数。

 

找对应的素数:

int find(int m,int left,int right){

	if (m<prime[left])// 考虑不再范围内的情况
	{
		return prime[left];
	}
	int mid=(left+right)/2;

	if(abs(right-left)<=1)
		return max(prime[right],prime[left]);
	if (m<prime[mid])
	{
		right=mid;
	    return find(m,left,right);
	}
	else 
	{
		left=mid;
		return find(m,left,right);
	}
}

大致是二分查找的思想,这里有一种情况要注意;当原来的数不再最小素数和最大素数的区间时特殊处理。

代码:

// pat-1078.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include"algorithm"
#include "iostream"
#include "memory"
#include "math.h"
using namespace std;
#define maxn  50000
int vis[maxn];
int is_prime[maxn];
int prime[maxn];
int cnt=0;
void init() {
	memset( vis, 0, sizeof( vis ) );
	memset( is_prime, 0, sizeof( is_prime ) );
	is_prime[0] = is_prime[1] = 1;
	int i, j;

	for ( i = 2; i < maxn; i++ ) {
		for ( j = 2; j * i < maxn; j++ )
			is_prime[i * j] = 1;
	}

   cnt = 0;

	for ( i = 2; i < maxn; i++ ) {
		if ( !is_prime[i] )
			prime[cnt++] = i;
	}
}
int find(int m,int left,int right){

	if (m<prime[left])// 考虑不再范围内的情况
	{
		return prime[left];
	}
	int mid=(left+right)/2;

	if(abs(right-left)<=1)
		return max(prime[right],prime[left]);
	if (m<prime[mid])
	{
		right=mid;
	    return find(m,left,right);
	}
	else 
	{
		left=mid;
		return find(m,left,right);
	}
}

int main()
{
	init();
	bool first=true;
	int m=0,n=0,sizet=0;
	cin>>m>>n;
	if (is_prime[m])//非素数
	{   
		int left=0,right=cnt-1;
		sizet=find(m,left,right);
	}
	else
	sizet=m;
	int *visit=new int[sizet];
	memset(visit,0,sizeof(int)*sizet);//开数组大小注意是sizet
	for (int i=0;i<n;i++)
	{
		int temp=0,h=0;
		cin>>temp;
		h=temp%sizet;
		int htemp=h;
		int cc=0.0;
		while (visit[h]==1&&cc<((sizet)/2))//发生冲突
		{   
			cc++;
			//h=(temp+cc*cc)%sizet;
			h=(htemp+cc*cc)%sizet;//二次试探
		}
		if(cc<((sizet)/2)&&(!visit[h]))
		{   
			if (first)
			{
				cout<<h;
				first=false;
			}
			else
				cout<<" "<<h;
			visit[h]=1;//已经被访问了 
		}
		else//不能
		{
			if (first)
			{
				cout<<"-";
				first=false;
			}
			else
				cout<<" "<<"-";
		}

	}
	cout<<endl;
	return 0;
}