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 |
C写的。堆排序加二分 700MS过。 代码写的不好请凑合看#include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct node{ char *orig; //原词 char *tran; //译词 }Word; int adjustSort(Word word[],int n,int start); int HeapSort(Word word[],int n); int QuickSort(Word word[],int left,int right); int DivSon( Word word[],int left,int right); int BinFind(Word word[],int n,char key[]); int main(){ Word *word; char init[30],*ch; int addres,i,n; word = ( Word * ) malloc( 100000 *sizeof( Word )); n = 0; while(gets(init) && init[0]!='\0'){ word[n].orig=(char *)malloc( 15 *sizeof(char)); word[n].tran=(char *)malloc( 15 *sizeof(char)); ch=strstr(init," "); addres = ch - init; strcpy(word[n].orig,&init[addres+1]); init[addres]='\0'; strcpy(word[n++].tran,init); // sscanf(init,"%s%s",&tra,&org); // strcpy(word[n].orig,org); // strcpy(word[n++].tran,tra); } word = (Word *)realloc(word,n*sizeof(Word)); HeapSort(word,n); // QuickSort(word,0,n-1); // for( i = 0 ; i < n ; i++) // printf("%s\t\t%s\n",word[i].orig,word[i].tran); while(scanf("%s",init)!=EOF){ addres=BinFind(word,n,init); if( addres == -1) printf("eh\n"); else printf("%s\n",word[addres].tran); } return 0; } int BinFind(Word word[],int n,char key[]){ int mid,left,right,j; left = 0 ; right = n-1; while ( left <= right){ mid = ( left +right )/2; j = strcmp(word[mid].orig,key); if( 0 == j){ return mid; }else{ if( 1== j) right = mid - 1; else left = mid + 1; } } return -1; } int HeapSort(Word word[],int n){ char *tmp; int i; for( i = n/2 -1;i >=0 ;i--) adjustSort(word,n,i); for( i = n-1; i>0;i--){ tmp = word[0].orig; word[0].orig=word[i].orig; word[i].orig=tmp; tmp = word[0].tran; word[0].tran=word[i].tran; word[i].tran=tmp; adjustSort(word,i,0); } return 0; } int adjustSort(Word word[],int n,int start){ int i ; char *tmp; while( 2 * start +1 < n){ i =2 * start +1; if( (i + 1) < n ){ if( strcmp(word[i].orig,word[i+1].orig) == -1){ i++; } } if( strcmp( word[ i ].orig,word[start].orig) == 1){ tmp = word[ i ].orig; word[i].orig = word[start].orig; word[start].orig= tmp; tmp = word[i].tran; word[i].tran=word[start].tran; word[start].tran=tmp; start = i; }else{ break; } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator