Poj3630 phone list [trie tree]

Source: Internet
Author: User
Phone list
Time limit:1000 ms   Memory limit:65536 K
Total submissions:34805   Accepted:9980

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:

  • Emergency 911
  • Alice 97, 625, 999
  • Bob 91 12 54 26

In this case, it's not possible to call Bob, because the central wocould 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 wocould not be consistent.

Input

The first line of input gives a single integer, 1 ≤T≤ 40, the number of test cases. Each test case startsN, The number of phone numbers, on a separate line, 1 ≤N≤ 10000. Then followsNLines 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

2391197625999911254265113123401234401234598346

Sample output

NOYES

Source

Nordic 2007

 

Question:

Give n phone numbers with a maximum of 10 digits and ask if there is no prefix where a phone number is another phone number. Output no; otherwise, output Yes

Ideas:

The initial idea is to first add all characters to the trie tree at a time in ascending order of the string length. If a node has been marked as a string, no is output. Such an idea is TLE. This is probably because the length of each string needs to be obtained first and then sorted to O (N ^ 2 logn )?

In fact, we do not need to sort. You only need to count the number of times the node appears when setting up the trie tree. In addition, the number of nodes in a path close to the root node must be greater than or equal to that away from the root node. After the creation is complete, do we find that the path in the tree for each string is all nodes with the number of times greater than 1. If all the nodes whose paths are greater than 1 are found, this string is the prefix of a string. Output No

 

  1 #include <iostream>  2 #include <set>  3 #include <cmath>  4 #include <stdio.h>  5 #include <cstring>  6 #include <algorithm>  7 #include <vector>  8 #include <queue>  9 #include <map> 10 using namespace std; 11 typedef long long LL; 12 #define inf 0x7f7f7f7f 13  14 int t, n; 15 const int maxn = 1e5 + 5; 16 int ed[maxn]; 17 int trie[maxn][10], tot = 1; 18 //vector<string>s; 19 //string s[maxn]; 20 char s[maxn][10]; 21  22 void insertt(char* str) 23 { 24     int len = strlen(str), p = 1; 25     for(int k = 0; k < len; k++){ 26         int ch = str[k] - ‘0‘; 27         if(trie[p][ch] == 0){ 28             trie[p][ch] = tot++; 29         } 30         p = trie[p][ch]; 31         ed[p]++; 32     } 33     //ed[p] = true; 34     //ed[p] = true; 35 } 36  37 bool searchh(char *str) 38 { 39     int len = strlen(str), p = 19; 40     for(int k = 0; k < len; k++){ 41         p = trie[p][str[k] - ‘0‘]; 42         if(ed[p] == 1){ 43             return false; 44         } 45     } 46     return true; 47 } 48  49 bool cmp(string a, string b) 50 { 51     return a.size() > b.size(); 52     //return strlen(a) > strlen(b); 53 } 54  55 int main() 56 { 57     scanf("%d", &t); 58     while(t--){ 59         tot = 1; 60         memset(trie, 0, sizeof(trie)); 61         memset(ed, 0, sizeof(ed)); 62         scanf("%d", &n); 63         for(int i = 0; i < n; i++){ 64             /*char c[10]; 65             string ch; 66             scanf("%s", c); 67             ch = c; 68             s.push_back(ch); 69             //cin>>s[i];*/ 70             scanf("%s", &s[i]); 71             insertt(s[i]); 72         } 73         //sort(s.begin(), s.end(), cmp); 74         /*for(int i = 0; i < n; i++){ 75             cout<<s[i]<<endl; 76         }*/ 77         bool flag = true; 78         for(int i = 0; i < n; i++){ 79             if(searchh(s[i])){ 80                 flag = false; 81                 break; 82             } 83             /*if(searchh((char*)s[i].data())){ 84                 printf("NO\n"); 85                 flag = false; 86                 break; 87             } 88             else{ 89                 insertt((char*)s[i].data()); 90             }*/ 91         } 92         if(flag){ 93             printf("YES\n"); 94         } 95         else{ 96             printf("NO\n"); 97         } 98     } 99     return 0;100 }

 

Poj3630 phone list [trie tree]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.