# Phone List HDU - 1671（字典树模板题）

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<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=1e9+7;
const double pi=acos(-1);
const double eps=1e-8;
char s[12];
int trie[N][10],tot;
bool vis[N],flag,vis1[N];//vis标记字符是否出现过，vis1标记字符是否是单词的结尾
void sert()
{
int root=0;
int len=strlen(s);
for(int i=0;i<len;i++)
{
int id=s[i]-'0';
if(!trie[root][id]) trie[root][id]=++tot;
root=trie[root][id];
if(vis[root]&&vis1[root]) flag=false;//如果这个字符出现过并且还是单词的结尾，说明存在前缀
if(i!=len-1) vis[root]=true;
}
if(!vis[root]) vis[root]=true;
else flag=false;//如果最后一个字符出现过，那么说明这个单词是一个前缀
vis1[root]=true;//标记这是一个单词的结尾
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
flag=true,tot=0;
memset(trie,0,sizeof(trie));
memset(vis,false,sizeof(vis));
memset(vis1,false,sizeof(vis1));
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s",s);
sert();
}
if(flag) printf("YES\n");
else printf("NO\n");
}
}

/*
3
3
911
97625999
91125426
5
113
12340
123440
12345
98346
3
97625999
91125426
911

NO
YES
NO
*/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=1e9+7;
const double pi=acos(-1);
const double eps=1e-8;
int trie[N][12],tot;
bool vis[N],flag;
void sert(char s[])
{
int root=0;
for(int i=0;s[i]!='\0';i++)
{
int id=s[i]-'0';
if(!trie[root][id]) trie[root][id]=++tot;
root=trie[root][id];
if(vis[root]) flag=false;
}
if(!vis[root]) vis[root]=true;
}
struct node
{
char ss[12];
int len;
}s[N];
bool cmp(node x,node y)
{
return x.len<y.len;
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
flag=true,tot=0;
memset(trie,0,sizeof(trie));
memset(vis,false,sizeof(vis));
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s",s[i].ss);
s[i].len=strlen(s[i].ss);
}
sort(s,s+n,cmp);
for(int i=0;i<n;i++) sert(s[i].ss);
if(flag) printf("YES\n");
else printf("NO\n");
}
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120