-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathq1.cpp
More file actions
75 lines (66 loc) · 1.48 KB
/
q1.cpp
File metadata and controls
75 lines (66 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using std::endl;
using std::cin;
using std::cout;
using std::sort;
using std::strcmp;
using std::getchar;
using std::scanf;
using std::printf;
// 1. 词典
// 根据题目建议使用了c语言的io,在vs2017会有warning,在预编译命令中加入_CRT_SECURE_NO_WARNINGS可消除warning
// 以下使用快排+二分,直接使用map也可以
struct Entry {
char forigen[12];
char english[12];
bool operator < (const Entry &entry) const {
return strcmp(forigen, entry.forigen) < 0;
}
};
Entry dict[100005];
int dictSize = 0;
int binarySearch(char target[12]) {
int res = -1;
int left = 0;
int right = dictSize - 1;
int middle = 0;
while (left <= right) {
int middle = (right - left) / 2 + left;
if (strcmp(dict[middle].forigen, target) < 0) {
left = middle + 1;
} else if (strcmp(dict[middle].forigen, target) > 0) {
right = middle - 1;
} else {
res = middle;
break;
}
}
return res;
}
int main(int argc, char *argv[]) {
dictSize = 0;
while (true) {
char initChar = getchar();
if (initChar == '\n') {
break;
}
dict[dictSize].english[0] = initChar;
scanf("%s %s", dict[dictSize].english + 1, dict[dictSize].forigen);
getchar();
dictSize++;
}
sort(dict, dict + dictSize);
char target[12];
while (scanf("%s", target) != EOF) {
int res = binarySearch(target);
if (res != -1) {
printf("%s\n", dict[res].english);
} else {
printf("eh\n");
}
}
return 0;
}