读书人

ZOJ 1109 Language of FatMouse(露骨的

发布时间: 2012-10-10 13:58:11 作者: rapoo

ZOJ 1109 Language of FatMouse(赤裸裸的字典树)
#include <iostream>#include <stdio.h>#include <cstring>#include <string>using namespace std;struct node{ char ch[12]; node *next[26];};node *root, memory[200005];int cnt;node *create(){ node *p = &memory[cnt++]; int i; for(i = 0; i < 26; i++) p->next[i] = NULL; return p;};void insert(char *s, char *c){ node *p = root; int i, k; for(i = 0; s[i]; i++) { k = s[i] - 'a'; if(p->next[k] == NULL) { p->next[k] = create(); } p = p->next[k]; } strcpy(p->ch, c);}void search(char *s){ node *p = root; int i, k; for(i = 0; s[i]; i++) { k = s[i] - 'a'; if(p->next[k] == NULL) { printf("eh\n"); return; } p = p->next[k]; } printf("%s\n", p->ch);}int main(){ char ch[15], sh[15], str[35]; root = create(); while(gets(str)) { if(str[0] == 0) break; sscanf(str, "%s %s", ch, sh); insert(sh, ch); } while(gets(ch)) { search(ch); } return 0;}

?

?

?

?

读书人网 >编程

热点推荐