zoukankan      html  css  js  c++  java
  • HDOJ 1671 Phone List

    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

      这道题就是判断一组字符串中是否有一个字符串是另一个字符串的前缀.

     
    #include <iostream>
    #include <string>
    #include <cstring>
    using namespace std;
    int k;
    typedef structnode
    {
        int num;
        structnode *next[10];
    }node;
    nodememory[1000000];
    
    int insert(char *s, node*T)
    {
        int len = 0;
        int id = 0;
        node*p,*q;
        p = T;
        len = strlen(s);
        for(int i = 0 ; i < len; i++)
        {
            id = s[i] - '0';
            if(p->num == 1)
              return 1;
            if(p->next[id] == NULL)
            {
                q = &memory[k++];
                q->num = 0;
                for(int j = 0; j < 10; j++)
                {
                    q->next[j] = NULL;
                }
    
                p->next[id] = q;
            }
            p = p->next[id];
        }
    
        for(int i = 0 ; i < 10; i++)
        {
            if(p->next[i] != NULL)
               return 1;
        }
        p->num = 1;
        return 0;
    }
    int main()
    {
        int t;
        node*T;
    
        cin>>t;
        while(t)
        {
            k = 0;
            T = &memory[k++];
            T->num = 0;
            for(int i = 0 ; i < 10; i++)
            {
                T->next[i] = NULL;
            }
            int flag = 0;
            int n;
            cin>>n;
            for(int i = 0 ; i < n ; i++)
            {
                char s[15];
                cin>>s;
                if(flag) 
                  continue;
                if(insert(s,T))
                  flag = 1;
            }
            if(flag)
              cout<<"NO"<<endl;
            else
              cout<<"YES"<<endl;
            t--;
        }
        return 0;
    }
  • 相关阅读:
    JSON.parse(JSON.stringify()) 实现对对象的深拷贝
    Promise 多重链式调用
    qs.parse() 和 qs.stringfy() 之 传输数据秘籍
    js 递归思想 处理后台多维数组的数据 之 完美契合
    js 反转字符串的实现
    js 中的! 和 !! 的区别
    vue 权限管理深度探究
    Web 前端 中高难度问题(希望看完之后的你可以拿到Offer^v^)
    vue懒加载 路由 router 的编写(resolve)
    Efficiency in Shell
  • 原文地址:https://www.cnblogs.com/T8023Y/p/3255107.html
Copyright © 2011-2022 走看看