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;
    }
  • 相关阅读:
    20190710-汉诺塔算法
    20190705-Python数据驱动之DDT
    20190621-N皇后
    还在为Excel合并单元格导致的各种问题烦恼吗?这里一起解决
    Excel基础:开始菜单之对齐方式,那些被遗忘的实用功能
    Excel中身份证号码如何分段显示,难倒小编,有什么好方法吗?
    制作这样的Excel注水图表,让老板另眼相看,坐等升职加薪
    Excel高手都会的Shift快捷键7个用法,让工作效率翻倍
    Excel答粉丝问:批量将单元格内容转为批注
    Excel基础:开始菜单之字体的华丽转身
  • 原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7125801.html
Copyright © 2011-2022 走看看