Phone List(C++ vector容器)

绝地灬酷狼 2022-08-02 07:02 356阅读 0赞

Phone List

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12940 Accepted Submission(s): 4405

Problem Description

Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let’s say the phone catalogue listed these numbers:

  1. Emergency 911
  2. Alice 97 625 999
  3. Bob 91 12 54 26
    In this case, it’s not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob’s phone number. So this list would not be consistent.

Input

The first line of input gives a single integer, 1 <= t <= 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 <= n <= 10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.

Output

For each test case, output “YES” if the list is consistent, or “NO” otherwise.

Sample Input

  1. 2
  2. 3
  3. 911
  4. 97625999
  5. 91125426
  6. 5
  7. 113
  8. 12340
  9. 123440
  10. 12345
  11. 98346

Sample Output

  1. NO
  2. YES

Source

2008 “Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(3)

  1. #include<cstdio>
  2. #include<vector>
  3. #include<iostream>
  4. #include<algorithm>
  5. #include<cstring>
  6. using namespace std;
  7. int main()
  8. {
  9. int t,n,i,flag;
  10. string s;
  11. cin>>t;
  12. while(t--)
  13. {
  14. vector<string>vec;
  15. cin>>n;
  16. flag=0;
  17. for(i=1;i<=n;i++)
  18. {
  19. cin>>s;
  20. vec.push_back(s);
  21. }
  22. sort(vec.begin(),vec.end());
  23. for(i=0;i<n-1;i++)
  24. {
  25. if(vec[i+1].find(vec[i])==0)//从开始找的!
  26. {
  27. cout<<"NO"<<endl;
  28. flag=1;
  29. }
  30. if(flag)
  31. {
  32. break;
  33. }
  34. }
  35. if(!flag)
  36. {
  37. cout<<"YES"<<endl;
  38. }
  39. }
  40. }

发表评论

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

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

相关阅读

    相关 Vector容器

    Vector 容器 一、 简介 vector 是将元素置于一个动态数组中加以管理的容器 vector可以随机存取元素(支持索引值直接存取,用\[\]操作符或at()函数)

    相关 Vector容器

    一、vector容器。 1、vector容器基本概念。 1)功能: vector容器数据结构与数组非常相似,也称为单端数组。 2)vector与普通数组区别?

    相关 c++容器vector介绍

    vector简介     vector是STL中最常见的容器,它是一种顺序容器,支持随机访问。vector是一块连续分配的内存,从数据安排的角度来讲,和数组极其相似,不同的地