POJ3630-静态字典树
来源:互联网 发布:ansys14.5软件下载 编辑:程序博客网 时间:2024/06/06 03:23
Description
Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogue listed these numbers:
- Emergency 911
- Alice 97 625 999
- Bob 91 12 54 26
In this case, it's not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob's phone number. So this list would not be consistent.
Input
The first line of input gives a single integer, 1 ≤ t ≤ 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 ≤ n ≤ 10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.
Output
For each test case, output "YES" if the list is consistent, or "NO" otherwise.
Sample Input
2391197625999911254265113123401234401234598346
Sample Output
NOYES
Source
静态字典树的实现:
一。我想首先要知道大概要提前开好多大的空间:比如这个题:
一行一个电话号码最多10个数字,现在有10000行,最坏的情况:每个数字都得开辟一个空间
那么就要 10 0000 个空间。
二。要知道怎么利用这些空间。
字典树的特征就是有 孩子节点 这点不能改变。
那么怎么给 孩子节点空间呢?
就像题目中的
list[now].a[s[i]-'0']=++ptr;
now=ptr;这个++ptr就相当于 new一样,但是它更看的清楚,因为它把“开辟”的空间编号都给出来了
再把now=ptr 就相当于 动态中的 p=p->next[i];一样。
这下 静态的字典树就很清晰了。
静态的字典树就是为了节省时间,采取了空间换时间的措施!
题意:就是不能有公共前缀。
输入一个串就处理一个,只有两种情况,要么它是前面串的前缀,或者它前面有它的前缀。
这题动态创建会超时。静态创建好数组,一组案例完了就要把前面用过的内存清空!
#include <string>//静态Trie#include <cstring>#include <cstdlib>#include <iostream>using namespace std;struct node{ bool end; //记录是不是到了该串串尾。 int a[10];//0~9 --- 10个数字};bool x;int ptr;node list[100000];void Init() //把100000的空间全部初始化!{ x=true; ptr=0; for(int i=0;i<100000;i++) { list[i].end=false; for(int j=0;j<10;j++) list[i].a[j]=-1; }}void Insert(char*s){ bool z=false; bool y=false; int now=0; int len=strlen(s); for(int i=0;i<len;i++) { if(list[now].a[s[i]-'0']==-1) { z=true; //表示这个串不是前面串的 前缀! list[now].a[s[i]-'0']=++ptr;//list提供空间,有100000个空间可供使用。.a[s[i]-'0']下标存储数字, //映射只要不是-1就代表这个空间已经被开了,映射++ptr和new一样给这个数字一个空间。++ptr就是它的空间编号 now=ptr; } else { now=list[now].a[s[i]-'0'];//把它的空间的键值给now if(list[now].end) y=true;//表示前面存在现在这个串的前缀。 } } list[now].end=true; x=(!y)&&z; } int main(){ int t,n; char s[11]; scanf("%d",&t); while(t--) { Init(); scanf("%d",&n); while(n--) { scanf("%s",&s); if(x) Insert(s);//只要有一个串存在了前缀串。就x=0,不要再插入了! } if(x) printf("YES\n"); else printf("NO\n"); } return 0;}
动态的超时!
#include<iostream>#include<cstdio>using namespace std;class Trie{public:int FLAG;Trie *next[11];Trie(){FLAG=0;memset(next,NULL,sizeof(next));}};int hash(char *s ,Trie &root){int flag1=0;Trie*p=&root;int len=0;while(s[len]!='\0'){int index=s[len++]-'0';if(!p->next[index]){p->next[index]=new Trie;flag1=1;}else {if(p->next[index]->FLAG) {return 0;}}p=p->next[index];}if(!flag1){return 0;}p->FLAG=1;return 1;}int main(){int T;scanf("%d",&T);while(T--){Trie root;int n;scanf("%d",&n);int flag=1,flag1=1;while(n--){char a[12];scanf("%s",a);int flag=hash(a,root);if(!flag) {flag1=0;}}if(flag1)printf("YES\n");else printf("NO\n");}return 0;}
- POJ3630-静态字典树
- poj3630 字典树
- POJ3630/HDU-1671 Phone List,字典树静态建树!
- poj3630(Phone List+字典树)
- Phone List(poj3630,字典树)
- POJ3630 Phone List(字典树)
- poj3630 Phone List 字典树
- POJ3630 Phone List(字典树)
- Trie树(字典树)poj3630+hdu1671
- HDU1671 poj3630 Phone List 字典树
- POJ3630 Phone List(字典树)
- poj3630—Phone List(字典树)
- POJ3630 trie字典树水题
- POJ3630 静态建立trie
- POJ3630 Phone List+字典数三种申请内存的方法--动态、静态、半动态
- ZOJ2876 POJ3630 HDU1671 Phone List,静态Trie树
- C++——字典树(Trie树)例题——Phone List(POJ3630)(HDU1671)
- POJ3630(Trie树)
- android的窗口机制分析------事件处理
- 递归练习
- hdu 1025 Constructing Roads In JGShining's Kingdom(最长上升序列n*logn算法)
- POJ 2377 最大生成树
- ffmpeg的使用
- POJ3630-静态字典树
- Tree.Panel各项属性
- HDU 2227 Find the nondecreasing subsequences (线段树)
- U盘启动安装Ubuntu 13.04
- Windows文件读写速度
- OpenCV 2 学习笔记(15): 绘制图像直方图
- 在Eclipse中的SVN提交时错误:Transaction is out of date
- Android关键资源详解
- spring配置事务之注解方式