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;
    }
  • 相关阅读:
    为什么说2013是PHP年
    wordpress 投稿插件 支持图片上传
    php简易页面内调试技巧
    WordPress中文文档
    百度网盘文件直链
    HOWTO:如何解决安装包在系统“添加/删除”中无法修复或卸载的问题
    InstallShield 2008 终止声明 (EOL)对最终客户意味着什么
    InstallShield 2011新功能试用(10) Express版本
    AdminStudio 9.5 Service Pack 3
    INFO:InstallShield中安装路径变量的区别
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3086639.html
Copyright © 2011-2022 走看看