Source Code Problem: 2503 User: zhouxc Memory: 4316K Time: 344MS Language: G++ Result: Accepted Source Code #include "iostream" #define N 100002 using namespace std; typedef struct Tran{ char E[20]; char F[20]; }; Tran L[N]; char ch[20]; int comp(const void *p1,const void *p2) { return strcmp((*(Tran*)p1).F,(*(Tran*)p2).F); } char* Binary_Search(char c[],int len) { int left,right,middle; left=0; right=len-1; while(left<=right) { middle=(left+right)/2; if(strcmp(c,L[middle].F)>0) left=middle+1; else if(strcmp(c,L[middle].F)<0) right=middle-1; else return L[middle].E; } return "eh"; } int main() { int cunt=0; while(1) { ch[0]=getchar(); if(ch[0]=='/n') break; scanf("%s%s",ch+1,L[cunt].F); getchar(); strcpy(L[cunt].E,ch); cunt++; } qsort(L,cunt,sizeof(L[0]),comp); // for(int i=0;i<cunt;i++) // printf("%s/n",L[i].F); while(scanf("%s",ch)!=EOF) printf("%s/n",Binary_Search(ch,cunt)); return 0; } Source Code Problem: 2503 User: zhouxc Memory: 5884K Time: 579MS Language: G++ Result: Accepted Source Code #include "iostream" #include "string.h" #include "malloc.h" using namespace std; typedef struct BitNode{ char str1[11]; char str2[11]; struct BitNode *Left; struct BitNode *Right; }BitNode,*BiTree; char ch[11],ch1[11],ch2[11]; void Creat(BiTree *T,char *ch1,char *ch2) { if(*T==NULL) { *T=(BitNode*)malloc(sizeof(BitNode)); strcpy((*T)->str1,ch1); strcpy((*T)->str2,ch2); (*T)->Left=NULL; (*T)->Right=NULL; } else { if(strcmp((*T)->str2,ch2)>=0) Creat(&(*T)->Left,ch1,ch2); else Creat(&(*T)->Right,ch1,ch2); } } char* Find(BiTree T,char *ch) { if(T!=NULL) { if(strcmp(T->str2,ch)>0) return Find(T->Left,ch); else if(strcmp(T->str2,ch)<0) return Find(T->Right,ch); else return T->str1; } return "eh"; } int main() { BiTree T=NULL; while(1) { ch1[0]=getchar(); if(ch1[0]=='/n') break; scanf("%s %s",ch1+1,ch2); getchar(); Creat(&T,ch1,ch2); } while(scanf("%s",ch)!=EOF) printf("%s/n",Find(T,ch)); // while(1); return 0; }