首页 > 其他 > 详细

山东省第一届ACM大学生程序设计竞赛(原题)—A—Phone Number

时间:2014-04-13 19:50:42      阅读:508      评论:0      收藏:0      [点我收藏+]
Phone Number
题目描述
We know that if a phone number A is another phone number B’s prefix, B is not able to be called. For an example, A is 123 while B is 12345, after pressing 123, we call A, and not able to call B.
Given N phone numbers, your task is to find whether there exits two numbers A and B that A is B’s prefix.

输入
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 phone numbers.

The last case is followed by a line containing one zero.


输出

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


示例输入
2
012
012345
2
12
012345

0


示例输出
NO

YES


字符串两两比较,判断一个串是否为另一个串的前缀。。


#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;

int cmpstr(string a,string b)
{
    for(int i=0;a[i]!=‘\0‘&&b[i]!=‘\0‘;i++)
        if(a[i]!=b[i])
            return 0;
    return 1;
}
int main()
{
    int n;
    string str[1002];
    while(cin>>n&&n)
    {
        int falg=1;
        for(int i=0;i<n;i++)
            cin>>str[i];

        for(int i=0;i<n&&falg;i++)
            for(int j=i+1;j<n;j++)
            {
                if(cmpstr(str[i],str[j]))
                {
                    falg=0;
                    cout<<"NO"<<endl;
                    break;
                }
            }
        if(falg==1) cout<<"YES"<<endl;
    }
    return 0;
}


山东省第一届ACM大学生程序设计竞赛(原题)—A—Phone Number,布布扣,bubuko.com

山东省第一届ACM大学生程序设计竞赛(原题)—A—Phone Number

原文:http://blog.csdn.net/lttree/article/details/23600489

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!