跟poj3349很类似的题目,这题还稍简单。用qsort快速排序和二分查找可以很轻松AC。以下是代码:
Run ID | User | Problem | Result | Memory | Time | Language | Code Length | Submit Time |
5135234 | zen_chou | 2503 | Accepted | 2356K | 547MS | C | 1212B | 2009-05-11 20:12:26 |


1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #define MAXSIZE 100001
5 typedef struct{
6 char english[11];
7 char foreign[11];
8 }ENTRY;
9 ENTRY entry[MAXSIZE];
10 int cmp(const void *a, const void *b){
11 return strcmp((*(ENTRY *)a).foreign, (*(ENTRY *)b).foreign);
12 }
13
14 int main()
15 {
16 int i, len_fre, count=0, low, high, mid, r, flag;
17 char c, message[11];
18 //freopen("input.txt", "r", stdin);
19 while ((c=getchar())!='\n'){
20 //读入数据
21 i=0;
22 len_fre=0;
23 while (c!=' '){
24 entry[count].english[i++]=c;
25 c=getchar();
26 }
27 entry[count].english[i]='\0';
28 c=getchar();
29 i=0;
30 while (c!='\n'){
31 entry[count].foreign[i++]=c;
32 c=getchar();
33 len_fre++;
34 }
35 entry[count].foreign[i]='\0';
36 count++;
37 }
38 //对entry按照foreign进行快速排序
39 qsort(entry, count, sizeof(entry[0]), cmp);
40 while (scanf("%s", message)!=EOF){
41 //对entry的foreign进行二分查找
42 low=0;
43 high=count-1;
44 flag=0;
45 while (low<=high){
46 mid=(low+high)/2;
47 r=strcmp(message, entry[mid].foreign);
48 if (r==0){
49 printf("%s\n", entry[mid].english);
50 flag=1;
51 break;
52 }
53 if (r<0){
54 high=mid-1;
55 }
56 else{
57 low=mid+1;
58 }
59 }
60 if (flag==0){
61 printf("eh\n");
62 }
63 }
64 }