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

     

  • 相关阅读:
    english note(6.3 to 6.8)
    english note(6.2 to 5.30)
    Lambda表达式
    Python Software Foundation
    eval(input())
    北航操作系统实验2019:Lab4-1代码实现参考
    北航操作系统实验2019:Lab4-1流程梳理
    面向对象设计与构造2019 第二单元总结博客作业
    面向对象设计与构造2019 第一单元总结博客作业
    Java代码度量分析工具:Designite简介
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6842325.html
Copyright © 2011-2022 走看看