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

我竟然用哈希做了,还wa了,帮看看吧~~水题大做,5555~~

Posted by 1152230301 at 2011-10-05 15:29:28 on Problem 1318
#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:
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