-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgk_lan1_bai3.c
55 lines (47 loc) · 1.18 KB
/
gk_lan1_bai3.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int check(const char *word, const char *hauto) {
int wordLen = strlen(word);
int hautoLen = strlen(hauto);
if (wordLen < hautoLen) {
return 0;
}
int wordIndex = wordLen - 1;
int hautoIndex = hautoLen - 1;
while (hautoIndex >= 0) {
if (word[wordIndex] != hauto[hautoIndex]) {
return 0;
}
wordIndex--;
hautoIndex--;
}
return 1;
}
int scmp(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b);
}
int main() {
char hauto[10];
char input[1000000];
scanf("%s", hauto);
getchar();
char *words[10001];
int cnt = 0;
while (fgets(input, sizeof(input), stdin) != NULL) {
char *token = strtok(input, " \n");
while (token != NULL && cnt < 10001) {
token[strcspn(token, "\n")] = '\0';
strcpy(words[cnt], token);
cnt++;
token = strtok(NULL, " ");
}
}
qsort(words, cnt, sizeof(char *), scmp);
for (int i = 0; i < cnt; i++) {
if (check(words[i], hauto)) {
printf("%s\n", words[i]);
}
}
return 0;
}