POJ -- 3630 Phone List(Trie 树)
来源:互联网 发布:开淘宝店要投入多少钱 编辑:程序博客网 时间:2024/05/21 21:41
题目大意:
t 组测试样例,每个样例有n个电话号码,问是否一个电话号码是另一个电话号码的前缀,是输出no,否输出yes;
注意:数据比较大,静态建树,统一分配内存。
代码实现:
①:排序,按照字符串的字典序,用string类型,直接进项sort排序就行(默认字典序顺序),避免这样的样例:
911111
911
字典序大的字符串在前面;
#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<iostream>using namespace std;bool flag;int id;struct tree{ bool flag; tree *next[10];}*root,node[100005];void Insert(string &number){ tree *p=root; int i=0,x; while(number[i]){ x=number[i++]-'0'; if(!(p->next[x])) p->next[x]=&node[++id]; p=p->next[x]; if(p->flag==1) flag=1; } p->flag=1;}int main(){ int t,n; scanf("%d",&t); string number[10005]; while(t--){ flag=id=0; memset(node,0,sizeof(node)); root=&node[0]; scanf("%d",&n); for(int i=0;i<n;++i) cin>>number[i]; sort(number,number+n); for(int i=0;i<n;++i) Insert(number[i]); if(!flag) cout<<"YES"<<endl; else cout<<"NO"<<endl; }}
②:不需要排序,既要在插入过程中,判断当前插入的位置时候是别的字符串的结束位置 / /1,又要在插入字符串后,判断当前位置是否还连接了别的字符/ / 2 ;
#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<iostream>using namespace std;bool flag;int id;struct tree{ bool flag; tree *next[10];}*root,node[100005];void Insert(string &number){ tree *p=root; int i=0,x; while(number[i]){ x=number[i++]-'0'; if(!(p->next[x])) p->next[x]=&node[++id]; p=p->next[x]; if(p->flag==1) flag=1;//1 } p->flag=1; for(int i=0;i<10;++i){ // 2 if(p->next[i]){ flag=1; break; } }}int main(){ int t,n; scanf("%d",&t); string number[10005]; while(t--){ flag=id=0; memset(node,0,sizeof(node)); root=&node[0]; scanf("%d",&n); for(int i=0;i<n;++i) cin>>number[i]; for(int i=0;i<n;++i) Insert(number[i]); if(!flag) cout<<"YES"<<endl; else cout<<"NO"<<endl; }}
0 0
- POJ 3630 - Phone List (Trie树)
- POJ -- 3630 Phone List(Trie 树)
- POJ 3630 Phone List 【Trie树入门】
- [poj 3630] Phone List trie 树
- poj 3630 Phone List(依旧Trie~)
- POJ 3630 - Phone List (Trie)
- POJ 3630 Phone List(Trie)
- poj 3630 Phone List (trie~)
- poj 3630 Phone List trie
- poj 3630 Phone List(Trie)
- POJ 3630 Phone List // Trie
- POJ 3630 Phone List(Trie树,静态数组实现)
- Trie树学习小记 Poj 3630 & Hdu1671 Phone List
- HDU 1671 && POJ 3630 Phone List (Trie树 好题)
- POJ 3630 Phone List(trie 前缀)
- POJ 3630 Phone List Trie题解
- Phone List(trie树)
- POJ-3630-Phone List-Trie树(键树、数字查找树、基数树)
- .tmux.conf
- 第14周-阅读项目5-字符串流对象
- Java类加载器总结
- 基于mindwave脑电波进行疲劳检测算法的设计(1)
- [leetcode] Combination Sum II
- POJ -- 3630 Phone List(Trie 树)
- 晶振,时钟PLL,CPU,APBI,TIM,电源,电路,看门狗等等stm32工作原理的通俗解释。
- 使用原生的NSXmlParser解析xml数据
- mysql 数据库to_days,str_to_date函数的使用
- 终端如何默认开启tmux
- 第14周-项目1-用二进制文件处理学生成绩
- 关于报错 No result defined for action.....and result input
- iSCSI的配置(target/initiator)
- Percona Xtradb Mysql Cluster学习(by quqi99)