zoukankan      html  css  js  c++  java
  • Phone List 南邮NOJ 1522

    Phone List

    时间限制(普通/Java) : 1000 MS/ 3000 MS          运行内存限制 : 65536 KByte
    总提交 : 108            测试通过 : 41 

    题目描述

    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 ≤ ≤ 40, the number of test cases. Each
    test case starts with n, the number of phone numbers, on a separate line, 1 ≤ ≤ 10000.
    Then follows 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

    提示

    undefined


    本题求解未使用更优化的数据结构~

    仅仅使用了string类中一些函数,比如strncmp等,我觉得这种解法反而更好些【渣渣捂脸哭~】

    思路就是:首先定义二维数组存储Phone List,然后进行qsort排序,这个排序很巧妙的把电话表按照数字对应的ASCII码进行从大到小、从短到长的排序,我们接下来就根据排序结果进行循环判断,如果前后两个字符串啊a[i],a[i+1]中,a[i+1[的前部分包含a[i]中的所有字符,那么就可以得出结论,该电话表不一致(not consistent)。这个判断通过strncmp(a[i],a[i+1],strlen(a[i])==0来实现。

    下面给出AC代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int comp(const void *a,const void *b)
    {
        return strcmp((char*)a,(char*)b);
    }
    int T;
    char phonelist[10000][11];
    int main()
    {
        scanf("%d",&T);  //测试样例个数
        while(T--)
        {
            int n;  //电话表个数
            scanf("%d",&n);
            for(int i=0;i<n;i++)
            {
                scanf("%s",phonelist[i]);
            }
            qsort(phonelist,n,sizeof(phonelist[0]),comp);
            bool flag=false;
            for(int i=0;i<n-1;i++)
            {
                if(strncmp(phonelist[i],phonelist[i+1],strlen(phonelist[i]))==0)
                {
                   flag=true;
                   break;
                }
            }
            if(flag==true)
            {
                printf("NO
    ");
            }
            else
            {
                printf("YES
    ");
            }
        }
        return 0;
    }
    


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    两个51相互之间单片机如何进行串口通信
    (stm32f103学习总结)—stm32pwm
    (stm32f103学习总结)—stm32 PMW输出实验
    cpu指令如何读写硬盘
    线程进程同步
    stm32+lwip
    opc
    open62541-server编程
    linux 下time函数
    close与shutdown
  • 原文地址:https://www.cnblogs.com/Tobyuyu/p/4965634.html
Copyright © 2011-2022 走看看