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 |
我竟然用哈希做了,还wa了,帮看看吧~~水题大做,5555~~#include<iostream> #include<algorithm> #include<string.h> #include<memory.h> #define N 100003 #define M 100 int cmp ( const void *a , const void *b ) { return *(char *)a - *(char *)b; } struct node { int hash; struct node*next; }*link[N]={NULL}; char word[105][M],q[105][M]; int ELFhash(char *key) { unsigned long h=0,g; while(*key) { h=(h<<4)+*key++; g=h&0xf0000000l; if(g) h^=g>>24; h&=~g; } return h%N; } struct pai{ int x,y; }s[105]; int cmp1( const void *a , const void *b ) { struct pai *c = (pai *)a; struct pai *d = (pai *)b; return c->y - d->y; } int main() { int i,e,n=0,t; char str[M]; struct node*p; while(scanf("%s",str)!=EOF&&strcmp(str,"XXXXXX")) { strcpy(word[n],str); qsort(str,strlen(str),sizeof(str[0]),cmp); e=ELFhash(str); p=(struct node*)malloc(sizeof(struct node)); p->hash=n; p->next=link[e]; link[e]=p; n++; } while(scanf("%s",&str)!=EOF&&strcmp(str,"XXXXXX")) { memset(s,0,sizeof(s)); t=0; qsort(str,strlen(str),sizeof(str[0]),cmp); e=ELFhash(str); p=link[e]; if(p==NULL) { printf("NOT A VALID WORD\n******\n"); continue; } while(p!=NULL) { strcpy(q[t],word[p->hash]); for(i=0;i<strlen(word[p->hash]);i++) s[t].y+=(100-i)*(word[p->hash][i]-'a'+1); s[t].x=t; p=p->next; t++; } qsort(s,t,sizeof(s[0]),cmp1); for(i=0;i<t;i++) printf("%s\n",q[s[i].x]); printf("******\n"); } //system("pause"); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator