Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
标准HASH~~ 本机多组自测数据没问题,但提交就WA! 哪位高手看看#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator