trie树

参考资料:

http://www.docin.com/p-48431870.html

poj练习:

trie树(静态建树、动态建树) 2513, 3630, 1204, 2503

以下是来自豆丁网的截图.

trie树

trie树

trie树

Trie树的实现.(这里只讨论简单Trie数)

1。结构体

//trie树结构体 struct Trie { Trie *next[26]; bool isWord; }Root;

2。插入操作

//插入操作(也是构建Trie树) void insert(char *tar) { Trie* head = &Root; int id; while(*tar) { id = *tar - 'a'; if(head->next[id] == NULL) head->next[id] = new Trie(); head = head->next[id]; tar++; } head->isWord = true; }

3。查找操作

//查找 bool search(char *tar) { Trie* head = &Root; int id; while(*tar) { id = *tar - 'a'; if(head->next[id] == NULL) return false; head = head->next[id]; tar++; } if(head->isWord) return true; else return false; }

poj 2503 解题报告

//trie #include<iostream> using namespace std; #define N 200000 struct trieNode { int next[26]; char word[12]; }; trieNode trie[N]; int len = 1; void insert(char* fw,char* tar) { trieNode * p = &trie[0]; int id; while(*tar) { id = *tar-'a'; if(p->next[id] == -1) { p->next[id] = len; len++; } p = &trie[p->next[id]]; tar++; } strcpy(p->word, fw); } char* search(char* tar) { trieNode* p = &trie[0]; int id; while(*tar) { id = *tar -'a'; if(p->next[id] == -1) return NULL; p = &trie[p->next[id]]; tar++; } if(strlen(p->word) > 0) return p->word; else return NULL; } int main() { char w[11], f[11], s[25]; int i, j; for(i=0; i<N; i++) { strcpy(trie[i].word,""); memset(trie[i].next, -1, sizeof(trie[i].next)); } while(gets(s) && s[0] != 0) { for(i=0; s[i]!=' '; i++) w[i] = s[i]; w[i] = 0; for(++i,j=0; s[i]; i++,j++) f[j] = s[i]; f[j] = 0; insert(w, f); } char* p; while(scanf("%s",&w) != EOF) { p = search(w); if(p != NULL) printf("%s/n", p); else puts("eh"); } return 0; } //hash /* #include<iostream> using namespace std; #define MAX 200000 struct Dic { char w[11], f[11]; }dic[MAX/2]; int hash[MAX]; int main() { int i, j, k=0, hashValue; char s[25]; memset(hash, -1, sizeof(hash)); while(gets(s) && s[0] != 0) { for(i=0; s[i]!=' '; i++) dic[k].w[i] += s[i]; dic[k].w[i] += 0; for(++i,j=0; s[i]!='/0'; i++,j++) dic[k].f[j] += s[i]; dic[k].f[j] += 0; i = hashValue = 0; while(dic[k].f[i]) { hashValue += hashValue*26 + dic[k].f[i++]; hashValue %= MAX; } while(hash[hashValue] != -1) hashValue = (hashValue+1)%MAX; hash[hashValue] = k; k++; } while(scanf("%s", s) != EOF) { i = hashValue = 0; while(s[i]) { hashValue += hashValue*26 + s[i++]; hashValue %= MAX; } while(strcmp(dic[hash[hashValue]].f, s) != 0 && hash[hashValue] != -1) hashValue = (hashValue+1)%MAX; if(hash[hashValue] == -1) puts("eh"); else printf("%s/n", dic[hash[hashValue]].w); } return 0; } */ //STL map /* #include<iostream> #include<map> #include<string> using namespace std; int main() { int i; char s[25], c; map<string, string> map; string w, f, tmp; while(gets(s) && s[0] != 0) { w = f = ""; for(i=0; s[i]!=' '; i++) w += s[i]; for(++i; s[i]!='/0'; i++) f += s[i]; map[f] = w; } while(scanf("%s", s) != EOF) { tmp = map[string(s)]; if(tmp.size() == 0) puts("eh"); else printf("%s/n", tmp.c_str()); } return 0; } */

poj 3630 Phone List

http://162.105.81.212/JudgeOnline/problem?id=3630

#include<iostream> using namespace std; #define MAX 100000 struct Trie { int next[10]; bool isPhoNum; }; Trie trie[MAX]; int len; void init() { len = 1; for(int i=0; i<MAX; i++) { memset(trie[i].next, -1, sizeof(trie[i].next)); trie[i].isPhoNum = 0; } } bool insert(char* tar) { Trie* head = &trie[0]; int id; while(*tar) { id = *tar - '0'; if(head->next[id] == -1) head->next[id] = len++; else if(trie[head->next[id]].isPhoNum) return 1; head = &trie[head->next[id]]; tar++; } head->isPhoNum = 1; for(id=0; id<10; id++) if(head->next[id] != -1) return 1; return 0; } int main() { int i, t, n, flag; char c[15]; scanf("%d", &t); while(t--) { init(); flag = 0; scanf("%d", &n); for(i=0; i<n; i++) { scanf("%s", c); if(insert(c)) { puts("NO"); for(++i; i<n ;i++) scanf("%*s"); flag = 1; break; } } if(!flag) puts("YES"); } return 0; }

poj 1035 Spell checker

http://162.105.81.212/JudgeOnline/problem?id=1035

#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #define MAXN 200002 struct Trie { int next[26]; bool isWord; }trie[MAXN]; int index; struct Dic { char word[16]; }dic[10001]; void init() { index = 1; for(int i=0; i<MAXN; i++) { memset(trie[i].next, -1, sizeof(trie[i].next)); trie[i].isWord = 0; } } void insert(char *tar) { Trie* root = &trie[0]; int id; while(*tar) { id = *tar - 'a'; if(root->next[id] == -1) root->next[id] = index++; root = &trie[root->next[id]]; tar++; } root->isWord = 1; } bool find(char *tar) { Trie *root = &trie[0]; int id; while(*tar) { id = *tar - 'a'; if(root->next[id] == -1) { /* if(*(tar+1) == 0 && root->isWord) return 1; else return 0; */ return 0; } tar++; root = &trie[root->next[id]]; } if(root->isWord) return 1; return 0; } bool isLike(char *tar, char *match) { int len_tar = strlen(tar); int len_match = strlen(match); if(abs(len_tar - len_match) >= 2) return 0; if(len_tar == len_match) //replace { int num = 0; for(int i=0; i<len_tar; i++) { if(num > 1) return 0; if(tar[i] != match[i]) num++; } if(num > 1) return 0; return 1; } else if(len_tar > len_match) //insert { bool vist = 0; int i, j; for(i=0,j=0; i<len_match; i++,j++) { if(tar[j] != match[i]) { if(vist) return 0; if(match[i] != tar[++j]) return 0; else vist = 1; } } return 1; } else //delete { int i, j; bool vist = 0; for(i=0, j=0; i<len_tar; i++,j++) { if(tar[i] != match[j]) { if(vist) return 0; if(match[++j] != tar[i]) return 0; else vist = 1; } } return 1; } } int main() { //freopen("in.txt", "r", stdin); int i = 0; char c[16]; init(); while(gets(dic[i].word) && dic[i].word[0] != '#') insert(dic[i++].word); while(gets(c) && c[0] != '#') { if(find(c)) printf("%s is correct/n", c); else { printf("%s:", c); for(int I=0; I<i; I++) { if(isLike(c, dic[I].word)) printf(" %s", dic[I].word); } printf("/n"); } } return 0; }