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 |
TLE 哪位好心人帮忙看一下瓶颈在那里,该怎么改进?#include<stdio.h> #include<string.h> /* * Macro */ #define MAX_WORD_NUM 100000 #define MAX_WORD_LENGTH 10+1 #define PRIME_NUM 13 /* * declaration of structure */ typedef struct HashTable_struct{ char value[MAX_WORD_LENGTH]; char key[MAX_WORD_LENGTH]; }HashTable; /* * declaration of functions */ int evalKey(char *p); void insertTable(char *key,char* value); int sfindValue(char *key); /* * global variables */ HashTable hashTable[MAX_WORD_NUM]; /* * program entry */ int main() { char english[MAX_WORD_LENGTH],dialect[MAX_WORD_LENGTH],line[MAX_WORD_LENGTH*2+1]; int index; while(1){ english[0]=(char)getchar(); if(english[0]=='\n')break; scanf("%s %s",english+1,dialect); getchar();/* eat the ending '\n'*/ /*insert this key-value pair into the hash table*/ insertTable(dialect,english); } while(scanf("%s",dialect)!=EOF){ index=findValue(dialect); if(index>=0) printf("%s\n",hashTable[index].value); else printf("eh\n"); } } /* * definition of functions */ /*hash the string*/ int evalKey(char *p){ int key=0; while(*p){ key+=(*p++)*PRIME_NUM; key=key%MAX_WORD_NUM; } return key; } /*insert the key-value pair into the hash table*/ void insertTable(char *key,char *value){ int keyNum=evalKey(key); while(hashTable[keyNum].key[0]!='\0'){ keyNum++; keyNum=keyNum%MAX_WORD_NUM; } strcpy(hashTable[keyNum].key,key); strcpy(hashTable[keyNum].value,value); } /*given a key, find the position of this key , return -1 if not found*/ int findValue(char *key){ int keyNum=evalKey(key); while(hashTable[keyNum].key[0]!='\0'){ if(strcmp(hashTable[keyNum].key,key)==0) return keyNum; keyNum++; keyNum=keyNum%MAX_WORD_NUM; } return -1; Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator