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;

    }

  • 相关阅读:
    ORA-04013,CACHE 值必须小于CYCLE值;解决方案
    .net 调用WCF服务接收数据对象属性为空
    各JAVA JDK版本下载地址
    记录一次基于Echart的数据可视化平台开发
    Swiper轮播使用记录--一个页面有多个DIV区域使用swiper进行轮播
    Echart使用记录-动态设置series的时候,设置了color属性,但所有的柱状图显示灰白色
    记录一次SignalR服务的搭建注意事项
    C#关于ListBox绑定list,不会刷新数据的问题
    关于bootstrap-fileinput上传后删除选中上传的文件,回到最初的选择文件界面
    Oracle 数据库 alert日志及trace日志的清理
  • 原文地址:https://www.cnblogs.com/yyroom/p/2940339.html
Copyright © 2011-2022 走看看