• HDU Phone List


    Phone List

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 6713    Accepted Submission(s): 2290


    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
     
    解析:字典树问题,判断一串号码中是否有一个号码为其它号码的前缀,是则输出“NO”,否则输出“YES”
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    
    typedef struct node
    {
        int n;
        node *next[10];
    }TreeNode;
    
    TreeNode *pHeadNode = NULL;
    char str[10005][10];
    
    void ZeroNode(TreeNode *&pHead)
    {
        for (int i = 0; i < 10; i++)
        {
            pHead->next[i] = NULL;
        }
    }
    
    void Insert(char *str)
    {
        int nLen = strlen(str);
        if (nLen == 0)
        {
            return;
        }
        TreeNode *pHead = pHeadNode;
        for (int i = 0; i< nLen; i++)
        {
            int index = str[i] - '0';
            if (pHead->next[index] == NULL)
            {
                TreeNode *newNode = (TreeNode *)malloc(sizeof(TreeNode));
                ZeroNode(newNode);
                newNode->n = 1;
                pHead->next[index] = newNode;
            }
            else
            {
                pHead->next[index]->n++;
            }
            pHead = pHead->next[index];
        }
    }
    
    int Find(char *str)
    {
        int nLen = strlen(str);
        if (nLen == 0)
        {
            return 0;
        }
        TreeNode *pHead = pHeadNode;
        for (int i = 0; i < nLen; i++)
        {
            int index = str[i] - '0';
            if (pHead->next[index] == NULL)
            {
                return 0;
            }
            else
            {
                pHead = pHead->next[index];
            }
        }
        return pHead->n;
    }
    
    void release(TreeNode *pHead)
    {
        for (int i = 0; i < 10; i++)
        {
            if (pHead->next[i] != NULL)
            {
                release(pHead->next[i]);
            }
        }
        free(pHead);
    }
    
    int main()
    {
        int N;
        int flag = 0;
        scanf("%d", &N);
        while(N--)
        {
            pHeadNode = (TreeNode *)malloc(sizeof(TreeNode));
            ZeroNode(pHeadNode);
            int n;
            scanf("%d", &n);
            flag = 0;
            for (int i = 0; i < n; i++)
            {
                scanf("%s", str[i]);
                Insert(str[i]);
            }
            for (int i = 0; i< n; i++)
            {
                if (Find(str[i]) > 1)
                {
                    flag = 1;
                    break;
                }
            }
            if (!flag)
            {
                printf("YES\n");
            }
            else
            {
                printf("NO\n");
            }
            release(pHeadNode);
        }
        return 0;
    }
  • 相关阅读:
    centos 7.1开机/etc/rc.local脚本不执行的问题
    ssh免密码登录之ssh-keygen的用法
    Centos 7.x临时的网络与路由配置
    Centos 7.x系统安装后的初始化配置
    U盘安装Centos7.1操作系统的问题记录
    linux系统中关于shell变量$*与$@的区别
    linux服务器init 5启动图形界面,报错Retrigger failed udev events
    rpmdb: unable to join the environment的问题解决
    BFS 典型的迷宫问题
    JUnit的基本使用
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3086639.html
Copyright © 2020-2023  润新知