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;
    }


  • 相关阅读:
    java接口对入参的判断校验
    sqlyog使用技巧
    mysql 数据库的表中复制一条数据并添加到该表中
    union all ,union 注意事项,查询结果集中的字段名称顺序必须一致
    IDEA连接mysq数据库,其实很简单
    git版本回退、git远程分支管理、git本地分支管理、git生产代码bug修复
    Vue上拉加载下拉刷新---vue-easyrefresh
    Flutter上拉加载下拉刷新---flutter_easyrefresh
    vue-cli webpack多Html页面的配置(附框架vue-webpack-multipage实例)
    Qt使用镜像源快速安装与更新
  • 原文地址:https://www.cnblogs.com/whileskies/p/7273496.html
Copyright © 2011-2022 走看看