Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

哪位大哥能帮我看看,是我的Hash用得不对还是这题不能用hash

Posted by bupt_09 at 2006-08-09 20:45:08 on Problem 2945
#include<stdio.h>
#include<string.h>
#include<memory.h>
const int maxn=54117;
const int maxs=20010;

int a[maxs],hash[maxn],b[maxs];
int n,m;
int ELFhash(char *key)
{
	unsigned long h=0;
	while(*key)
	{
		h=(h<<4)+*key++;
		unsigned long g=h&0Xf0000000L;
		if(g)
			h^=g>>24;
		h&=~g;
	}
	return h%maxn;
}	

int main()
{
	int i,key,cn;
	char s[23];
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		if(n==0)
			break;
		cn=0;
		memset(hash,255,sizeof(hash));
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		for(i=0;i<n;i++)
		{
			scanf("%s",s);
			key=ELFhash(s);
			if(hash[key]==-1)
			{
				hash[key]=cn;
				a[hash[key]]++;
				cn++;
			}
			else
				a[hash[key]]++;
		}
		for(i=0;i<cn;i++)
			b[a[i]]++;
		for(i=1;i<=n;i++)
			printf("%d\n",b[i]);
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator