zoukankan      html  css  js  c++  java
  • Phone List

    http://acm.hdu.edu.cn/showproblem.php?pid=1671

    Phone List

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 20211    Accepted Submission(s): 6836


    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
     方法一:字典树(注意字符串最后的判断)
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <cmath>
    #include <vector>
    #include <set>
    #include <map>
    #include <algorithm>
    using namespace std;
    struct trie
    {
        bool isend;
        struct trie *next[10];
    };
    trie *root;
    bool flag;
    void buildtrie(trie *root,char *s)
    {
        trie *p=root;
        trie *tmp;
        int len=strlen(s);
        for(int i=0;i<len;i++)
        {
            if(p->next[(int)(s[i]-'0')]==NULL){
                tmp=new trie();
                for(int j=0;j<10;j++)
                    tmp->next[j]=NULL;
                tmp->isend=false;
                p->next[(int)(s[i]-'0')]=tmp;
            }
            else
            {
                if(p->next[s[i]-'0']->isend==true)
                {
                    flag=false;
                    return;
                }
                else if(i==len-1 && p->next[s[i]-'0']!=NULL)
                {
                    flag=false;
                    return ;
                    /*
                    测试数据
                    1
                    3
                    97625999
                    91125426
                    911
                     */
                }
            }
            p=p->next[s[i]-'0'];
        }
        p->isend=true;
    }
    void del(trie *root)
    {
        if(root==NULL) return;
        for(int i=0;i<10;i++)
        {
            if(root->next[i]!=NULL)
                del(root->next[i]);
        }
        free(root);
    }
    int main()
    {
        int t,n;
        char a[15];
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            int x=0;
            root=new trie();
            root->isend=false;
            for(int i=0;i<10;i++)
                root->next[i]=NULL;
            flag=true;
            for(int i=0;i<n;i++)
            {
                scanf("%s",a);
                if(flag==true)buildtrie(root,a);
            }
            if(flag==false) printf("NO
    ");
            else printf("YES
    ");
            del(root);
        }
        return 0;
    }

    方法二:放入容器内排序查找

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <cmath>
    #include <vector>
    #include <set>
    #include <map>
    #include <algorithm>
    using namespace std;
    int main()
    {
        vector<string>v;
        char a[10];
        string s;
        int t,n;
        scanf("%d",&t);
        while(t--)
        {
            cin>>n;
            v.clear();
            for(int i=0;i<n;i++)
            {
                scanf("%s",a);
                s=a;
                v.push_back(s);
            }
            sort(v.begin(),v.end());
            for(int i=0;i<n-1;i++)
            {
                if(v[i+1].find(v[i])==0)
                {
                    printf("NO
    ");
                    goto eg;
                }
            }
            printf("YES
    ");
            eg:;
        }
        return 0;
    }
  • 相关阅读:
    无重复字符的最长子串
    最长公共前缀
    项目开发的 工程化
    包管理 import debug 模块管理 module
    Third Party Browser Drivers NOT DEVELOPED by seleniumhq
    任何不看源码的代码引入都是存在定时爆炸的可能
    博客数计数
    lineage 世系 血缘 容错机制 DAG
    查源码分析 游标 写 需要 cursors 一切不看源码的代码引入都是定时炸弹的启动
    8核 16g 及时释放内存空间
  • 原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7125801.html
Copyright © 2011-2022 走看看