zoukankan      html  css  js  c++  java
  • nyist 163 Phone List

    Phone List

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:4
    描述

    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.

    输入
    The first line of input gives a single integer, 1 ≤ t ≤ 10, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 ≤ n ≤ 100000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.
    输出
    For each test case, output "YES" if the list is consistent, or "NO" otherwise.
    样例输入
    2
    3
    911
    97625999
    91125426
    5
    113
    12340
    123440
    12345
    98346
    样例输出
    NO
    YES

    思路:
    字典树的使用,将我们的数字采用字符串进行保存,然后进行字典树创建,用一个k来表示是否是一个数字的结尾(是/否 1/0)
    typedef struct treenode
    {
        int k;
        treenode *a[10];
        treenode()
        {
            k = 0;
            int i = 0;
            for(i = 0; i < 10; i++)
            {
                a[i] = NULL;
            }
        }
    }treenode;

    判断分为两种情况
    1. 字符串在进行字典树创建时,在前面都有创建,那么这个满足题目的NO
    2. 在进行一个字符串创建字典树的时候发现创建的时候有一个字符串的结尾标志,那么这个已是NO

    解释不是很清楚,重在自己理解

    代码:

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>

    using namespace std;

    const int N=100005;

    typedef struct treenode
    {
        int k;
        treenode *a[10];
        treenode()
        {
            k = 0;
            int i = 0;
            for(i = 0; i < 10; i++)
            {
                a[i] = NULL;
            }
        }
    }treenode;

    treenode *head;

    int set_tree(char ch[])
    {
        int k = 0;
        int flag  = 1;
        treenode *p;
        p = head;
        int i = 0;
        for(i = 0; i < strlen(ch); i++)
           {
               int y = (int)ch[i]-48;
               if(p->a[y] == NULL)
               {
                   treenode *x;
                   x = new treenode;
                   p ->a[y] = x;
                   p = x;
               }
               else
               {
                   k++;
                   p = p->a[y];
                   if(p->k==1)
                   {
                      // printf("%s\n",ch);
                       return 1;
                       flag = 0;
                       break;
                   }
               }
           }
        if(i == strlen(ch))
          {
              p ->k = 1;
          }
         // printf(" k =%d\n",p->k);
        if(flag)
        {
        if(k==strlen(ch))
            return 1;
        else
            return 0;
        }
    }

    char ch[N][12];

    int main(void)
    {
        int n;
        scanf("%d",&n);
        while(n--)
        {
            int m;
            scanf("%d",&m);

             int i = 0,j = 0;
             for(i = 0; i < m; i++)
                scanf("%s",ch[i]);
            int flag = 1;
            head = new treenode;
            for(i = 0; i < m; i++)
              {
                     // printf("///%d\n",set_tree1(ch[i]));
                    if(set_tree(ch[i])==1)
                        {
                                printf("NO\n");
                                flag = 0;
                                break;
                        }
              }

            if(flag)
              printf("YES\n");
        }
        return 0;

    }

  • 相关阅读:
    Go基础---->go的基础学习(三)
    java基础---->Java关于复制的使用(一)
    java基础---->Reference的使用(一)
    java基础---->Java中枚举的使用(一)
    Go基础---->go的基础学习(一)
    java基础---->java中变参函数的使用
    java框架---->RxJava的使用(一)
    android基础---->AccessibilityService的简单使用(一)
    spring基础---->spring自定义标签(一)
    十分钟让你明白Objective-C的语法(和Java、C++的对比)
  • 原文地址:https://www.cnblogs.com/yyroom/p/2940339.html
Copyright © 2011-2022 走看看