zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Android之Handler实现延迟执行
    static{}语句块
    (转)git常见错误
    smarty使用
    git简易操作
    angular路由——ui.route
    angular服务二
    angular服务一
    angualr 实现tab选项卡功能
    EF 集合版 状态版的 增加、删除、修改 的实践
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3086639.html
Copyright © 2011-2022 走看看