# hdu1671（trie树（字典树））

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:

1. Emergency 911

2. Alice 97 625 999

3. 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

2 3 911 97625999 91125426 5 113 12340 123440 12345 98346

Sample Output

NO YES

``````#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int NODE = 1e5+10,CH = 26;
/****
INPUT:
(Lower case)

OUTPUT:

*/

struct Trie
{
int ch[NODE][CH],sz,val[NODE];
/**
ch[u][c]:   节点u指向的c儿子的边
val[u]:     节点u的值
sz:         trie树的size
*/
inline int idx(char c)
{
return c-'a';
}
int node()
{
/**
新建(初始化)节点
**/
memset(ch[sz],0,sizeof(ch[sz]));
///将所有儿子置为空
val[sz]=0;
return sz++;
}
void init()
{
sz=0;
///trie树根节点为下标0的节点
node();
}
void insert(char *s,int v)
{
int u=0;
for(;*s;s++)
{
int c=idx(*s);
///如果节点不存在 新建节点 并把值赋给当前节点的c儿子边
if(!ch[u][c])
ch[u][c]=node();
///继续移动
u=ch[u][c];
}
///在末尾节点记录信息
val[u]=v;
}
int find(char *s)
{
int u=0;
for(;*s;s++)
{
int c=idx(*s);
///如果u节点没有c儿子  结束
if(!ch[u][c])
return 0;
u=ch[u][c];
}
return val[u];
}
}soul;

char s[10000];

int main()
{
int n,m;
soul.init();
scanf("%d%d\n",&n,&m);
for(int i=1;i<=n;i++)
scanf("%s",s),soul.insert(s,i);
while(m--)
{
scanf("%s",s);
printf("%d\n",soul.find(s));
}
return 0;
}
``````

she

his

hers 构造Trie树 可以得到下图

``````#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int NODE = 1e6+10,CH = 26;

int ch[NODE][CH],sz,val[NODE],num[NODE];
char a1[100001][15],a2[15];
/**
ch[u][c]:   节点u指向的c儿子的边
val[u]:     节点u的值
sz:         trie树的size
*/
int idx(char c)
{return c-'0';}
int node()
{
/**
新建(初始化)节点
**/
memset(ch[sz],0,sizeof(ch[sz]));
///将所有儿子置为空
val[sz]=0;
return sz++;
}
void init()
{
sz=0;
///trie树根节点为下标0的节点
node();
}
void insert(char *s,int v)
{
int u=0;
num[u]++;
for(;*s;s++)
{

int c=idx(*s);
///如果节点不存在 新建节点 并把值赋给当前节点的c儿子边
if(!ch[u][c])
ch[u][c]=node();
///继续移动
//  cout<<u<<' '<<char(c+'a')<<' '<<ch[u][c]<<endl;
u=ch[u][c];num[u]++;
}
///在末尾节点记录信息
val[u]=v;
}
int find(char *s)
{
int u=0;
for(;*s;s++)
{
int c=idx(*s);
// cout<<u<<' '<<(char)(c+'a')<<' '<<num[u]<<endl;
///如果u节点没有c儿子  结束
if(!ch[u][c])
return 0;
u=ch[u][c];
// cout<<(char)*s<<endl;
}
return num[u];
}
int main()
{
int t;scanf("%d",&t);
while(t--)
{
init();
memset(num,0,sizeof(num));
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s",a1[i]);
insert(a1[i],i+1);
}
int flag=1;
for(int i=0;i<n;i++)
{
if(find(a1[i])>1)
{printf("NO\n");flag=0;break;}
}
if(flag==1)printf("YES\n");
}
return 0;
}``````

原文作者：Trie树
原文地址: https://blog.csdn.net/martinue/article/details/47443581
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。