标签:ace www com www. oid pac else nbsp algorithm
模板 :
#include<string.h> #include<stdio.h> #include<malloc.h> #include<iostream> #include<algorithm> using namespace std; const int maxn = 26; struct Trie { Trie *Next[maxn]; int v; inline void init(){ this->v = 1; for(int i=0; i<maxn; i++) this->Next[i] = NULL; } }; Trie *root = (Trie *)malloc(sizeof(Trie)); void CreateTrie(char *str) { int len = strlen(str); Trie *p = root, *tmp; for(int i=0; i<len; i++){ int idx = str[i]-‘a‘; if(p->Next[idx] == NULL){ tmp = (Trie *)malloc(sizeof(Trie)); tmp->init(); p->Next[idx] = tmp; }else p->Next[idx]->v++; p = p->Next[idx]; } p->v = -1;//若为结尾,则将v改成-1,当然字典树里面的变量都是要依据题目 //设置并且在特定位置赋值的 } int FindTrie(char *str) { int len = strlen(str); Trie *p = root; for(int i=0; i<len; i++){ int idx = str[i]-‘a‘; p = p->Next[idx]; //...进行一系列操作 } } inline void DelTrie(Trie *T) { if(T == NULL) return ; for(int i=0; i<maxn; i++){ if(T->Next[i] != NULL) DelTrie(T->Next[i]); } free(T); return ; } int main(void) { root.init();//!!! //... }
字典树算法参考 ==> http://www.cnblogs.com/tanky_woo/archive/2010/09/24/1833717.html
标签:ace www com www. oid pac else nbsp algorithm
原文地址:http://www.cnblogs.com/Rubbishes/p/7604516.html