Phone Number
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
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.
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.
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 <bits/stdc++.h>
using namespace std;
char str[1010][100100];
int main()
{
int n;
while(~scanf("%d",&n) && n)
{
bool flag = false;
for(int i=0; i<n; i++)
{
scanf("%s",str[i]);
if(!flag)
{
for(int j=0; j<i; j++)
{
bool f = 0;
for(int e = 0; str[i][e] && str[j][e]; e++)
{
if(str[i][e] != str[j][e])
{
f = 1;
break;
}
}
if(!f)
{
flag = 1;
break;
}
}
}
}
if(flag)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
}

本文介绍了一种用于判断电话号码列表中是否存在前缀冲突的算法。通过逐个比较电话号码,该算法能够确定是否有一个号码是另一个号码的前缀,从而避免在实际呼叫过程中发生错误。文章提供了完整的代码实现及示例。
2194

被折叠的 条评论
为什么被折叠?



