poj -- 2001 Shortest Prefixes (Trie 树)
2015-06-20 16:18
288 查看
题目大意:
求一字符串在一堆字符串中的能被辨别的最短前缀;
代码实现:
求一字符串在一堆字符串中的能被辨别的最短前缀;
代码实现:
#include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> using namespace std; char str[1005][25]; struct tree{ int cnt; tree *next[26]; }*root; tree *Create(){ tree *p; p=(tree *)malloc(sizeof(tree)); p->cnt=0; for(int i=0;i<26;++i) p->next[i]=NULL; return p; } void Insert(char *s){ int i=0,x,l=strlen(s); tree *p=root; while(i<l){ x=s[i++]-'a'; if((p->next[x])==NULL) p->next[x]=Create(); p=p->next[x]; p->cnt++; } } void Search(char *s){ tree *p=root; int i=0,x,l=strlen(s); while(i<l){ printf("%c",s[i]); x=s[i++]-'a'; p=p->next[x]; if((p->cnt)==1) break; } printf("\n"); } int main(){ int n=0; root=Create(); while(~scanf("%s",str )){ Insert(str ); n++; } for(int i=0;i<n;++i){ printf("%s ",str[i]); Search(str[i]); } }
相关文章推荐
- Hadoop集群(第6期)_WordCount运行详解
- Android ListView 单条刷新方法实践及原理解析
- bzoj3672
- PHP数组与对象之间用递归递归转换
- Hadoop集群(第5期)_Hadoop安装配置
- Pascal's Triangle
- 【BUAA 591】The Last Alpha Star
- Hadoop集群(第4期)_SecureCRT使用
- IO流学习二
- 最小生成树
- android应用的单元测试
- Esper入门简介:二 个人对它的理解
- 毕业课题之----HOG+SVM相关函数的解释
- SQL Server 有关EXCEPT和INTERSECT使用
- Bash的类型(交互、登录)及其相关配置文件
- 第四次作业:《国际贸易学》—关税壁垒措施
- Hadoop集群(第3期)_VSFTP安装配置
- linux程序设计——MySQL管理(第八章)
- WPF学习(二) - 绑定
- 统计学习方法——CART, Bagging, Random Forest, Boosting