字典树_HDU1671
来源:互联网 发布:外汇数据下载 编辑:程序博客网 时间:2024/06/06 02:08
题目链接:Phone List
思路:字典树模板题。不需要用到Search函数,用全局变量flag标记结果输出YES还是NO。当一个字符串在参与建Trie树时,走过的路上的点有isStr标记为true就证明此前参与建Trie树的一个字符串是当前串的前缀;还有当前参与建Trie树的串,最后一个字符所在的节点,它有子节点证明当前串是某串前缀。分别处理一下两种情况:如样例中,先输入911,后输入91 12 54 26 与先输入 91 12 54 26 后输入 911 处理方式不同。详见代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<queue>#include<stack>#include<vector>#include<cmath>#include<map>#include<set>#include<cstdlib>#define mem(a,b) memset(a,b,sizeof(a))#define INF 0x7ffffffftypedef long long ll;using namespace std;const int maxn = 1e4+10;const int MAX = 10;struct Trie{ bool isStr; Trie *next[MAX];};bool flag;void BuildTrie(Trie *root, const char *s){ if(root == NULL || *s == '\0'){ return; } int i; Trie *p = root; while(*s != '\0'){ if(p -> next[*s - '0'] == NULL){ Trie *tmp = (Trie *)malloc(sizeof(Trie)); for(int i = 0; i < MAX; i++){ tmp -> next[i] = NULL; } tmp -> isStr = false; p -> next[*s - '0'] = tmp; p = p -> next[*s - '0']; }else if(p -> next[*s - '0'] != NULL){ p = p -> next[*s - '0']; if(*(s+1) == '\0'){ flag = false; return; } if(p -> isStr == true){ flag = false; return; } } s++; } p -> isStr = true;}void dele(Trie *root){ int i; for(int i = 0; i < MAX; i++){ if(root -> next[i] != NULL){ dele(root -> next[i]); } } free(root);}//int Search(Trie *root,const char *s){// Trie *p = root;// while(p != NULL && *s != '\0'){// p = p -> next[*s - 'a'];// s++;// }// return (p != NULL && p -> isStr == true);//}int main(){ int T,i; scanf("%d",&T); while(T--){ int n; char s[maxn]; Trie *root = (Trie *)malloc(sizeof(Trie)); for(int i = 0 ;i < MAX; i++){ root -> next[i] = NULL; } root -> isStr = false; flag = true; scanf("%d",&n); getchar(); for(int i = 0; i < n; i++){ scanf("%s",s); if(flag) BuildTrie(root,s); } if(flag) puts("YES"); else puts("NO"); dele(root); } return 0;}
阅读全文
0 0
- 字典树_HDU1671
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树。。
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- UVA
- 观察者模式
- Apache Hadoop2.8.0伪分布式环境搭建
- tomcat启动startup.bat一闪而过
- Fecshop 环境部署以及安装步骤
- 字典树_HDU1671
- dlib 06 dlib自带demo 视频内物体跟踪
- C语言顺序表
- mac上面搭建php环境
- Spring Cloud初探——服务治理Spring Cloud Eureka
- 不区分字母大小写排序
- 豆瓣社区:《如何高效学习》应用与心得
- Echarts绘制折线图柱状图混合图表练习
- java中的对象游离