zoukankan      html  css  js  c++  java
  • [hdu 1671] Phone List

    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. 

    InputThe 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.OutputFor 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

    题意:判断某个字符串是全部字符串中的某个字符串的前缀,有输出YES, 反之NO
    思路:利用Trie来做,可以变插入变检测,如果在插入过程中有标记节点,则之前某个字符串是该字符串的前缀,如果插入完成之后,其后有子数,则该字符串是某个字符串的前缀。
       每组Case做完后,要释放空间,再建立新树。
    #include <iostream>
    #include <stdio.h>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    #define Max 10
    typedef struct TrieNode *Trie;
    struct TrieNode
    {
        Trie next[Max];
        int cnt;
        TrieNode()
        {
            for (int i = 0; i < Max; i++) {
                next[i] = NULL;
                cnt = 0;
            }
        }
    };
    
    bool Insert(Trie root, char *s)
    {
        Trie p = root;
        for (int i = 0; s[i]; i++) {
            int t = s[i]-'0';
            if (p->next[t] == NULL)
                p->next[t] = new TrieNode;
            p = p->next[t];
            if (p->cnt >= 1) return 1;
        }
        p->cnt++;
        for (int i = 0; i < Max; i++) {
            if (p->next[i] != NULL)
                return 1;
        }
        return 0;
    }
    
    void FreeTrie(Trie root)
    {
        for (int i = 0; i < Max; i++) {
            if (root->next[i] != NULL)
                FreeTrie(root->next[i]);
        }
        delete root;
    }
    
    int main()
    {
        //freopen("1.txt", "r", stdin);
        int T;
        scanf("%d", &T);
        while (T--) {
            int n;
            char s[10010][20];
            scanf("%d", &n);
            Trie root = new TrieNode;
            int flag = 0;
            for (int i = 0; i < n; i++) {
                scanf("%s", s[i]);
                if (Insert(root, s[i]))
                    flag = 1;
            }
            
            if (flag) printf("NO
    ");
            else printf("YES
    ");
            FreeTrie(root);
        }
    
    
        return 0;
    }


  • 相关阅读:
    创建大顶堆
    大顶堆的实现
    二叉树的前序,中序,后序,层序实现
    链表实现多项式相加
    225. Implement Stack using Queues
    232. Implement Queue using Stacks
    JavaScript 实现队列操作
    Vue 路由守卫
    回文数 & {}[]() 符号判断

  • 原文地址:https://www.cnblogs.com/whileskies/p/7273496.html
Copyright © 2011-2022 走看看