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 |
二分家快排#include <stdio.h> #include <string.h> #include <stdlib.h> #define M 100000+10 typedef struct node { char s1[15]; char s2[15]; }Node; Node word[M]; int n; int cmp(const void *_a,const void *_b) { Node *a=(Node*)_a; Node *b=(Node*)_b; return strcmp(a->s2,b->s2); } void find(char s[]) { int low=0,high=n,mid,flag; flag=0; while(low<=high) { mid=(low+high)/2; if(!strcmp(s,word[mid].s2)) { flag=1; printf("%s\n",word[mid].s1); break; } else if(strcmp(s,word[mid].s2)>0) low=mid+1; else if(strcmp(s,word[mid].s2)<0) high=mid-1; } if(!flag) printf("eh\n"); } int main() { char query[15]; char a[30],b[15],c[15]; while(gets(a)&&a[0]!='\0') { sscanf(a,"%s%s",&b,&c); strcpy(word[n].s1,b); strcpy(word[n++].s2,c); } qsort(word,n,sizeof(Node),cmp); while(gets(a)&&a[0]!='\0') { sscanf(a,"%s",&query); find(query); } return 1; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator