Phone Number

一时失言乱红尘 2022-08-18 02:35 108阅读 0赞

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.

输入

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.

示例输入

  1. 2
  2. 012
  3. 012345
  4. 2
  5. 12
  6. 012345
  7. 0

示例输出

  1. NO
  2. YES

提示

来源

2010年山东省第一届ACM大学生程序设计竞赛

示例程序

  1. #include<iostream>
  2. #include<string>
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<stdlib.h>
  6. using namespace std;
  7. /*struct node
  8. {
  9. int num;
  10. char name[10000];
  11. }a[1010],b;
  12. int cmp(const void *a,const void *b)
  13. {
  14. return (*(struct node *)a).num>(*(struct node *)b).num?1:-1;
  15. }*/
  16. string a[1005],k;
  17. int b[1005];
  18. int main()
  19. {
  20. int n,i,j,p,s;
  21. while(scanf("%d",&n)&&n)
  22. {
  23. s=0;
  24. for(i=0;i<n;i++)
  25. /*{
  26. scanf("%s",a[i].name);
  27. a[i].num=strlen(a[i].name);
  28. }
  29. qsort(a,n,sizeof(a[0]),cmp);*/
  30. //gets(a[i]);
  31. //scanf("%s",a[i]);
  32. cin>>a[i];
  33. for(i=0;i<n-1;i++)
  34. for(j=0;j<n-1-i;j++)
  35. {
  36. if(a[j]>a[j+1])
  37. {
  38. k=a[j];
  39. a[j]=a[j+1];
  40. a[j+1]=k;
  41. }
  42. }
  43. for(i=0;i<n;i++)
  44. b[i]=a[i].size();
  45. for(i=0;i<n-1;i++)
  46. {
  47. p=b[i]<b[i+1]?b[i]:b[i+1];
  48. for(j=0;j<p;j++)
  49. if(a[i][j]!=a[i+1][j])
  50. break;
  51. else if(j==p-1)
  52. s=1;
  53. }
  54. if(s==1)
  55. printf("NO\n");
  56. else
  57. if(s==0)
  58. printf("YES\n");
  59. }
  60. return 0;
  61. }

发表评论

表情:
评论列表 (有 0 条评论,108人围观)

还没有评论,来说两句吧...

相关阅读