字典树模板
typedef struct letter{ int finished = 0; char le; letter *son, *ne; letter():le('\0'),son(NULL),ne(NULL){}; }letter,*pointer; struct Trie { pointer rt; Trie() {rt = new letter();} void insert(char *word, int len) { pointer u = rt; for(int i = 0; i < len; i ++) { pointer v = u->son; if(v != NULL) while(v->ne != NULL && v->le != word[i]) v = v->ne; if(v == NULL || v->ne == NULL && v->le != word[i]) { pointer &p = (v == NULL ? u->son :v->ne); p = new letter(); p->le = word[i]; if(i == len - 1) ++ p->finished; u = p; } else u = v; } } bool find(char *word, int len) { pointer u = rt; for(int i = 0; i < len; i ++) { pointer v = u->son; if(v == NULL) return false; while(v->ne != NULL && v->le != word[i]) v = v->ne; if(v->ne == NULL && v->le != word[i]) return false; u = v; } return true; } };