zoukankan      html  css  js  c++  java
  • Phone List POJ 3630 Trie Tree 字典树

    Phone List
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 29416   Accepted: 8774

    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:

    • Emergency 911
    • Alice 97 625 999
    • 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

    Source

     

    开内存池!

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<sstream>
    #include<algorithm>
    #include<queue>
    #include<deque>
    #include<vector>
    #include<cmath>
    #include<map>
    #include<stack>
    #include<set>
    #include<fstream>
    #include<memory>
    #include<list>
    #include<string>
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    #define MAXN  10003
    #define LLL 1000000000
    #define INF 1000000009
    #define eps 0.00000001
    /*
    给定一些字符串,求这些字符串中是否存在一个字符串是另一个字符串的前缀
    用trie tree解决
    在插入时,分两种情况:
        1.当前字符串的长度小于Tree中字符串长度 ,s[i] == ''的情况
        2.和当前字符串长度大于或等于Tree中字符串长度,在Tree中每个字符串结束的地方标记一下!
    */
    bool flag,first;//这个标志是不是已经产生 上述情况
    char s[MAXN];
    typedef struct TreeNode
    {
        bool Endflag;
        struct TreeNode* Next[10];
    }*Tree;
    TreeNode Memory[1000000];
    int alloc = 0;
    Tree Newnode()
    {
        Tree T = &Memory[alloc++];
        for (int i = 0; i < 26; i++)
        {
            //T->Next[i] = (Tree)malloc(sizeof(TreeNode));
            T->Next[i] = NULL;
            T->Endflag = false;
        }
        return T;
    }
    Tree Insert(char s[], Tree t)
    {
        int p = 0;
        Tree T = t;
        while (s[p])
        {
            int k = s[p] - '0';
            if (!T->Next[k])
                T->Next[k] = Newnode();
            else
            {
                if (T->Next[k]->Endflag||(!T->Next[k]->Endflag&&s[p+1]==''))
                {
                    flag = true;
                    return t;
                }
            }
            T = T->Next[k];
            p++;
        }
        T->Endflag = true;
        return t;
    }
    int main()
    {
        int cas;
        scanf("%d", &cas);
        while (cas--)
        {
            Tree T = Newnode();
            flag = false; first = true;
            int n;
            scanf("%d", &n);
            while (n--)
            {
                scanf("%s", s);
                if (flag) continue;
                Insert(s, T);
            }
            if (!flag)
                printf("YES
    ");
            else
                printf("NO
    ");
        }
        return 0;
    }

     

  • 相关阅读:
    2-SAT模板
    AC自动机
    省选预备营-Day3(图论) 总结
    省选预备营-Day2(分治) 总结
    左偏树(可并堆)总结
    省选预备营-Day1(数据结构) 总结
    OI基础知识
    C++ 堆
    CH4601 普通平衡树
    java 函数形参传值和传引用的区别
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6842325.html
Copyright © 2011-2022 走看看