POJ 2503 Babelfish
字典树,WA;
----------------------------------------------------
# include <stdio.h> # include <string.h> # define LEN 12 char buf[2 * LEN], str[LEN]; struct word{ char a[LEN]; } ; struct tree{ tree *node[26]; char bj; word *pw; tree() { for (int i = 0; i < 26; ++i) node[i] = NULL; bj = 1; pw = NULL; } } *root; void build(tree *p, char *s) { char c = (*s) - 'a'; if ((p->node[c]) == NULL) { p->node[c] = new tree; } p = p->node[c]; if (s[2]) build(p, s+1); else { p->bj = 0; if (p->pw == NULL){ p->pw = new word; sscanf(buf, "%s", p->pw);} } } void visit(tree *p, char *s) { char c = (*s) - 'a'; p = p->node[c]; if (p == NULL) puts("eh"); else if (s[2]) visit(p, s+1); else if (p->bj==0 && !s[2]) puts(p->pw->a); else puts("eh"); } void release(tree *p) { for (int i = 0; i < 26; ++i) if (p->node[i]) release(p->node[i]); delete p->pw; delete p; } void init(void) { root = new tree; while (1) { gets(buf); if (!buf[0]) return ; sscanf(buf, "%*s%s", str); build(root, str); } } void solve(void) { while (~scanf("%s", str)) { visit(root, str); } release(root); } int main() { init(); solve(); return 0; }
----------------------------------------------------