首页 技术 正文
技术 2022年11月18日
0 收藏 913 点赞 4,150 浏览 2222 个字

Phone List

Problem DescriptionGiven 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.
 InputThe 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. OutputFor 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

 Source2008
“Insigma International Cup” Zhejiang Collegiate Programming Contest – Warm Up(3)

解题思路:

推断输入的串中是否存在某个串是另外串的前缀。

建立字典树,关键是设立某个串结尾的标志,即在哪个字母结尾。

要推断是否存在前缀要考虑两种情况。一是当前输入的串是否是曾经输入的串的前缀,而是曾经输入的串是否是当前输入的串的前缀。前一种情况在查找该串时,会从第一个字母查找到最后一个字母,中间不返回不论什么值,说明当前的串是曾经的前缀,后一种情况。当在查找当前串的时候遇到曾经串结束的标志。则说明曾经串是当前串的前缀。

代码中结束的标志为保存串中最后一个字母的那个节点的cnt值为-1.

如图:

代码:

#include <iostream>
#include <malloc.h>
#include <algorithm>
#include <string.h>
#include <stdio.h>
using namespace std;
const int maxn=10;
bool ok;
char str[12];
int t,n;struct Trie
{
int cnt;
Trie *next[maxn];
};Trie *root;void CreateTrie(char *str)
{
int len=strlen(str);
Trie*p=root,*q;
for(int i=0;i<len;i++)
{
int id = str[i]-'0';
if(p->next[id] == NULL)
{
q = (Trie *)malloc(sizeof(Trie));
q->cnt = 1;
for(int j=0; j<maxn; ++j)
q->next[j] = NULL;
p->next[id] = q;
p = p->next[id];
}
else
{
p = p->next[id];
}
}
p->cnt=-1;//串末尾的标志
}int findTrie(char *str)
{
int len=strlen(str);
Trie *p=root;
for(int i=0;i<len;i++)
{
int id=str[i]-'0';
if(p->next[id]==NULL)
return 0;//没有建过树
if(p->next[id]->cnt==-1)
return -1;//曾经串是当前串的前缀
p=p->next[id];
}
return -1;//当前串是曾经串的前缀
}void release(Trie *root)//释放空间
{
for(int i=0;i<maxn;i++)
{
if(root->next[i])
release(root->next[i]);
}
free(root);
}
int main()
{
scanf("%d",&t);
while(t--)
{
ok=1;
root=(Trie*)malloc(sizeof(Trie));//root为指针类型,须要申请空间
for(int i=0; i<10; ++i)
root->next[i] = NULL;
scanf("%d",&n);
while(n--)
{
scanf("%s",str);
if(findTrie(str)==-1)
ok=0;//有前缀
if(!ok)
continue;//有前缀,后面的就不用建树了
CreateTrie(str);
}
if(ok)
printf("YES\n");
else
printf("NO\n");
release(root);
}
return 0;
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,905
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,430
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727