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~~ 本机多组自测数据没问题,但提交就WA! 哪位高手看看

Posted by gyttry at 2007-05-20 16:55:10 on Problem 2503
#include "stdio.h"
#include "string.h"
unsigned long hash(char *arKey, unsigned int nKeyLength) 
{ 
	unsigned long h = 0, g; 
	char *arEnd=arKey+nKeyLength;  
	while (arKey < arEnd) 
	{ 
		h = (h << 4) + *arKey++; 
		if ((g = (h & 0xF0000000)))
		{ 
			h = h ^ (g >> 24); 
			h = h ^ g; 
		} 
	} 

	return arKey[0]-'a'; 
} 
char engword[100010][12];
char forword[100010][12];
struct _node
{
	int engidx;
	int foridx;
};
_node hashtable[500][10000];
char nword[500];
int main()
{
	freopen("c:\\acm\\data.txt","r",stdin);
	char str[12],ch;
	int engidx=0,foridx=0,hashidx,i;
	ch=getchar();
	while(ch!='\n')
	{
		i=0;
		while(ch<='z'&&ch>='a')
		{
			engword[engidx][i++]=ch;
			ch=getchar();
		}
		engword[engidx][i]=0;
		while(ch>'z'||ch<'a') ch=getchar();
		i=0;
		while(ch<='z'&&ch>='a')
		{
			forword[foridx][i++]=ch;
			ch=getchar();
		}
		forword[foridx][i]=0;
		hashidx=(hash(forword[foridx],strlen(forword[foridx])))%500;
		hashtable[hashidx][nword[hashidx]].engidx=engidx;
		hashtable[hashidx][nword[hashidx]].foridx=foridx;
		nword[hashidx]++;
		engidx++;
		foridx++;
		while(ch!='\n') ch=getchar();
		ch=getchar();
	}
	while(scanf("%s",str)!=EOF)
	{
		hashidx=(hash(str,strlen(str)))%500;
		for(int i=0;i<nword[hashidx];i++)
			if(strcmp(str,forword[hashtable[hashidx][i].foridx])==0)
				break;
		if(i<nword[hashidx])
			printf("%s\n",engword[hashtable[hashidx][i].engidx]);
		else
			printf("eh\n");
	}
}

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