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;
    }
  • 相关阅读:
    thinkphp3.2.3版本在windows本地apache环境运行正常,上传到centos服务器apache环境中出现:thinkphp 上传根目录不存在!请尝试手动创建:uploads/
    [POI2013]LUK-Triumphal arch
    【背包问题】
    2016 acm香港网络赛 A题. A+B Problem (FFT)
    tomcat部署项目的三种方式
    仿照ArrayList自己生成的MyList对象
    使用回调函数实现回文判断
    关于angularjs的model的一些问题
    关于使用Tomcat服务器出现413错误的解决办法(Request Entity Too Large)
    关于angularjs+typeahead的整合
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3086639.html
Copyright © 2011-2022 走看看