字典树简单题。
#include <cstdio>
#include <cstring>
#include <cstdlib> typedef struct Trie {
Trie *next[];
char str[];
} Trie; Trie root; void create(char str[], char des[]) {
int i = , j, id;
Trie *p = &root, *q; while (str[i]) {
id = str[i] - 'a';
if (p->next[id] == NULL) {
q = (Trie *)malloc(sizeof(Trie));
for (j=; j<; ++j)
q->next[j] = NULL;
p->next[id] = q;
}
p = p->next[id];
++i;
}
strcpy(p->str, des);
} void find(char str[]) {
int i = , id;
Trie *p = &root; while (str[i]) {
id = str[i] - 'a';
if (p->next[id] == NULL) {
printf("eh\n");
return ;
}
p = p->next[id];
++i;
}
printf("%s\n", p->str);
} void del(Trie *t) {
int i;
if (t == NULL)
return ;
for (i=; i<; ++i)
del(t->next[i]);
free(t);
} int main() {
char src[], des[];
char *p; for (int i=; i<; ++i)
root.next[i] = NULL; while (gets(des)!=NULL && des[]!='\0') {
p = strchr(des, ' ');
strcpy(src, p+);
*p = '\0';
create(src, des);
} while (scanf("%s", src) != EOF)
find(src); return ;
}