SDNU 1135.Phone Number(水题)

桃扇骨 2021-12-22 14:09 313阅读 0赞

Description

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.

Input

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.

Output

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

Sample Input

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

Sample Output

  1. NO
  2. YES

Source

山东省ACM 2010年第一届省赛

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cmath>
  4. #include <string>
  5. #include <cstring>
  6. #include <algorithm>
  7. #include <queue>
  8. #include <vector>
  9. #include <map>
  10. using namespace std;
  11. #define ll long long
  12. int n;
  13. string s[1000+8];
  14. int main()
  15. {
  16. while(~scanf("%d", &n) && n != 0)
  17. {
  18. for(int i = 0; i<n; i++)
  19. cin>>s[i];
  20. bool flag = 1;
  21. for(int i = 0; i<n; i++)
  22. {
  23. for(int j = i+1; j<n; j++)
  24. {
  25. int len1 = s[i].size(), len2 = s[j].size();
  26. for(int k = 0; k<min(len1, len2); k++)
  27. {
  28. if(s[i][k] == s[j][k])
  29. {
  30. flag = 0;
  31. break;
  32. }
  33. }
  34. if(!flag)break;
  35. }
  36. if(!flag)break;
  37. }
  38. if(!flag)printf("NO\n");
  39. else printf("YES\n");
  40. }
  41. return 0;
  42. }

转载于:https://www.cnblogs.com/RootVount/p/10985742.html

发表评论

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

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

相关阅读

    相关 SDNU 1049.盒饭(

    Description 有n个饭盒放成一排,其中第m个饭盒里多了一块牛肉。鲁观特别想吃带牛肉的那盒饭,但食堂阿姨说他只能先打开第一个饭盒,然后再向前移动两个饭盒,再向前移