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;
    }
  • 相关阅读:
    世界上最快的排序算法——Timsort
    十二种排序包你满意(冒泡、插入、归并、快速排序等包含希尔和计数排序)
    二叉树遍历方法大全(包含莫里斯遍历)
    Nginx知多少系列之(一)前言
    .NET Core项目部署到Linux(Centos7)(一)前言
    Nginx知多少系列之(十四)Linux下.NET Core项目Nginx+Keepalived高可用(主从模式)
    Nginx知多少系列之(七)负载均衡策略
    Nginx知多少系列之(六)Linux下.NET Core项目负载均衡
    Nginx知多少系列之(五)Linux下托管.NET Core项目
    Nginx知多少系列之(四)工作原理
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3086639.html
Copyright © 2011-2022 走看看