• Sdut 2151 Phone Numbers (山东省ACM第一届省赛题 A)


    题目描述

    We know thatif a phone number A is another phone number B’s prefix, B is not able to becalled. For an example, A is 123 while B is 12345, after pressing 123, we callA, and not able to call B.
    Given N phone numbers, your task is to find whether there exits two numbers Aand B that A is B
    sprefix.
     

    输入

     The input consists of several test cases.
     The first line of input in each test case contains one integer N (0<N<1001),represent the number of phone numbers.
     The next line contains N integers, describing the phonenumbers.
     The last case is followed by a line containing one zero.

    输出

     For each test case, if there exits a phone number thatcannot be called, print “NO”, otherwise print “YES” instead.

    示例输入

    2

    012

    012345

    2

    12

    012345

    0

    示例输出

    NO

    YES

    /*********************

    N个字符串中如果有一个是另外一个的前缀,则输出NO,否则输出YES。

    O(n^2)的循环,

    *********************/

    Code:

    #include <iostream>
    #include<string.h>
    #include<stdio.h>
    using namespace std;
    int main()
    {
        char  str[1005][1005];
        char s[1005];
        int n,i,j,li,lj;
        bool leaf;
        while(scanf("%d",&n)&&n)
        {
            leaf  = false;
            li = lj = 0;
            for(i = 0;i<n;i++)
                cin>>str[i];
            for(i = 0;i<n;i++)
            {
                li = strlen(str[i]);
                for(j = 0;j<n;j++)
                {
                    lj = strlen(str[j]);
                    if(i==j||li>lj)//自己不和自己比较,str[j] 长度比str[i]短的话肯定不是前缀。
                        continue;
                    strncpy(s,str[j],li);
                    s[li] = '';
                    if(strcmp(s,str[i])==0)// 发现前缀则跳出循环
                    {
                        leaf = true;
                        break;
                    }
                }
                if(leaf)
                    break;
            }
            if(leaf)
                printf("NO
    ");
            else
                printf("YES
    ");
        }
        return 0;
    }
    
    

  • 相关阅读:
    Lexical Sign Sequence
    (UPCOJ暑期训练)Tally Counters
    (2019hdu多校第十场) Welcome Party
    (2019hdu多校第十场1003) Valentine's Day
    更新,线段树模板(支持相关基本操作)
    linux(deepin)下Clion的安装及环境配置
    2019牛客第7场——C(Governing sand)
    【数论】数论之旅:N!分解素因子及若干问题
    [二分]Kayaking Trip
    [数论之旅]数学定理
  • 原文地址:https://www.cnblogs.com/gray1566/p/3704313.html
Copyright © 2020-2023  润新知