[ACM] hdu 1671 Phone List (特里)

Phone List

Problem 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

Source

2008
“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;
}

版权声明:本文博主原创文章,博客,未经同意,不得转载。

时间: 2024-10-25 16:18:59

[ACM] hdu 1671 Phone List (特里)的相关文章

HDU 1671 Phone List (Trie树 好题)

Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11721    Accepted Submission(s): 3982 Problem Description Given a list of phone numbers, determine if it is consistent in the sense th

(字典树)HDU - 1671 Phone List

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 题意:给很多电话号码,如果在拨号的时候,拨到一个存在的号码,就会直接打出去,以致以这个号码为前缀的所有其他比这个号码长的号码将不能拨出,问是不是所有的号码都能拨. 分析:可以直接建立字典树,节点中用boolean变量表示当前是否组成电话号码,一旦在遍历完某条号码之前,已经出现存在号码,则发现问题,返回false,否则true. 我使用了一个反过来的方法,即只统计前缀出现次数,遍历完某条号码,如

HDU 1671 (字典树统计是否有前缀)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem 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. Emergenc

字典树模板+HDU 1671 ( Phone List )(字典树)

字典树指针模板(数组模板暂时还没写): 1 #include<cstdio> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 const int MAX=26; 6 const int maxn=1e4+100; 7 int N; 8 9 struct Trie 10 { 11 Trie *next[MAX]; 12 int v;///v要灵活使用,具体情况具体分析 13 }; 14

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

[ACM] hdu 2089 不要62(数位Dp)

不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 19043    Accepted Submission(s): 6442 Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就

[ACM] hdu 1253 胜利大逃亡 (三维BFS)

胜利大逃亡 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在T分钟后回到城堡,Ignatius每分钟能从一个坐标走到相邻的六个坐标中的其中一个.现在给你城堡的地图,请你计算出Ignatius能否在魔王回来前离开城堡(只要走到出

[ACM] hdu 1285 确定比赛名次 (拓扑排序)

确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10358    Accepted Submission(s): 4046 Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直

[ACM] hdu 1231 最大连续子序列 (动规复习)

最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17687    Accepted Submission(s): 7828 Problem Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j