trie树

本文详细介绍了Trie树的基本概念及其在不同场景下的应用,包括静态建树和动态建树的方法,并提供了多个POJ平台上的例题解析,帮助读者深入理解Trie树的实现与使用。

参考资料:

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

poj练习:

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

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

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; }

本设计项目聚焦于一款面向城市环保领域的移动应用开发,该应用以微信小程序为载体,结合SpringBoot后端框架与MySQL数据库系统构建。项目成果涵盖完整源代码、数据库结构文档、开题报告、毕业论文及功能演示视频。在信息化进程加速的背景下,传统数据管理模式逐步向数字化、系统化方向演进。本应用旨在通过技术手段提升垃圾分类管理工作的效率,实现对海量环保数据的快速处理与整合,从而优化管理流程,增强事务执行效能。 技术上,前端界面采用VUE框架配合layui样式库进行构建,小程序端基于uni-app框架实现跨平台兼容;后端服务选用Java语言下的SpringBoot框架搭建,数据存储则依托关系型数据库MySQL。系统为管理员提供了包括用户管理、内容分类(如环保视频、知识、新闻、垃圾信息等)、论坛维护、试题与测试管理、轮播图配置等在内的综合管理功能。普通用户可通过微信小程序完成注册登录,浏览各类环保资讯、查询垃圾归类信息,并参与在线知识问答活动。 在设计与实现层面,该应用注重界面简洁性与操作逻辑的一致性,在满足基础功能需求的同时,也考虑了数据安全性与系统稳定性的解决方案。通过模块化设计与规范化数据处理,系统不仅提升了管理工作的整体效率,也推动了信息管理的结构化与自动化水平。整体而言,本项目体现了现代软件开发技术在环保领域的实际应用,为垃圾分类的推广与管理提供了可行的技术支撑。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值