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 |
APHASH 188ms#include <iostream> #include <cstring> using namespace std; struct node{ char trans[11]; char ori[11]; node *next; }; node head[99992]; node heap[100000]; int cursor; unsigned int APHash(char *str) { unsigned int hash = 0; int i; for (i=0; *str; i++) { if ((i & 1) == 0) { hash ^= ((hash << 7) ^ (*str++) ^ (hash >> 3)); } else { hash ^= (~((hash << 11) ^ (*str++) ^ (hash >> 5))); } } return (hash & 0x7FFFFFFF)%99991; } int main(void) { char buf[30]; int i=0,j; while (gets(buf) && buf[0]){ char *itr=buf; j=0; while (*itr!=' ') heap[i].trans[j++]=*itr++; heap[i].trans[j]=0; itr++;j=0; while (*itr) heap[i].ori[j++]=*itr++; heap[i].ori[j]=0; int h=APHash(heap[i].ori); if (head[h].next){ node *n=head[h].next; while (n->next) n=n->next; n->next=&heap[i]; }else head[h].next=&heap[i]; i++; } while (gets(buf)){ int h=APHash(buf); node *n=head[h].next; while (n && strcmp(n->ori,buf)) n=n->next; if (n) printf("%s\n",n->trans); else printf("eh\n"); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator