HDU 1671 Phone List(字典树模板)

Dear 丶 2022-09-28 14:24 259阅读 0赞










Phone List

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




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
    3
    911
    97625999
    91125426
    5
    113
    12340
    123440
    12345
    98346





 



Sample Output








  1. NO
    YES





 



Source

  1. #define _CRT_secure_NO_WARNINGS
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <string>
  6. #include <algorithm>
  7. using namespace std;
  8. char str[10005][15];
  9. struct Trie
  10. {
  11. int cnt;
  12. struct Trie *next[10];
  13. Trie():cnt(0)
  14. {
  15. memset(next,0,sizeof(next));
  16. }
  17. };
  18. Trie *root = NULL;
  19. void Trie_insert(char *str)
  20. {
  21. Trie *p = root;
  22. for(int i=0;i<strlen(str);i++)
  23. {
  24. if(p -> next[str[i] - '0'] == NULL)
  25. {
  26. p -> next[str[i] - '0'] = new Trie;
  27. }
  28. p = p -> next[str[i] -'0'];
  29. p ->cnt++;
  30. }
  31. }
  32. int Trie_search(char *str)
  33. {
  34. Trie *p = root;
  35. for(int i=0;i<strlen(str);i++)
  36. {
  37. p = p -> next[str[i] - '0'];
  38. }
  39. if(p -> cnt != 1)
  40. return 0;
  41. return 1;
  42. }
  43. void del(Trie *root)
  44. {
  45. if(root == 0)
  46. return;
  47. for(int i=0;i<10;i++)
  48. {
  49. if(root -> next[i])
  50. del(root -> next[i]);
  51. }
  52. delete root;
  53. }
  54. int main()
  55. {
  56. int t;
  57. cin>>t;
  58. while(t--)
  59. {
  60. int n;
  61. cin>>n;
  62. root = new Trie;
  63. for(int i=1;i<=n;i++)
  64. {
  65. cin>>str[i];
  66. Trie_insert(str[i]);
  67. }
  68. bool flag = true;
  69. for(int i=1;i<=n;i++)
  70. {
  71. if(!Trie_search(str[i]))
  72. {
  73. flag = false;
  74. break;
  75. }
  76. }
  77. if(flag)
  78. cout<<"YES"<<endl;
  79. else
  80. cout<<"NO"<<endl;
  81. del(root);
  82. }
  83. return 0;
  84. }

发表评论

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

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

相关阅读

    相关 hdu——1671 phone list

    首先想到的是用字典树存储号码,然后去查找每个输入的号码,判断是否存在一个单词词尾的节点值大于1;这就意味着他是某个单词的前缀,不过很不辛TLE...... inclu