Hash开散列 拉链法



#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=1000007;
struct node{
	__int64 real;
	int sum;
	node *next;
}myhash[maxn];
int get_hash(__int64 real)
{
	int hash;
	hash=real%maxn;
	if(hash<0)
		hash+=maxn;
	node *p=&myhash[hash];
	while(1)
	{
		if(p->sum==0)
		{
			p->real=real;
			p->sum=1;
			return p->sum;
		}
		else
		if(p->real==real)
		{
			p->sum++;
			return p->sum;
		}
		if(p->next==NULL)
			break;
		p=p->next;
	}
	p->next=new node;
	p=p->next;p->real=real;p->sum=1;p->next=NULL;
	return p->sum;
}

int main(){
	 int n;
	 int i;
	__int64 real;
	scanf("%d",&n);
	memset(myhash,0,sizeof(myhash));
	for(i=1;i<=n;i++)
	{
	scanf("%I64d",&real);
	printf("%d
",get_hash(real));
	}
	return 0;

}


版权声明:本文为博主原创文章,未经博主允许不得转载。

today lazy . tomorrow die .
原文地址:https://www.cnblogs.com/france/p/4808614.html