POJ 3630/hdu 1671 Phone List "Trie dictionary tree dynamic Creation & static Creation"

Source: Internet
Author: User
Tags cmath

Phone List
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 25160 Accepted: 7641

Description

Given a list of phone numbers, determine if it is consistent in the sense this 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 would direct your-to-the-emergency line as soon as Y OU had dialled the first three digits of Bob ' s phone number. So the list would not being consistent.

Input

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

Output

For each test case, the output "YES" If the list is consistent, or "NO" otherwise.

Sample Input

2391197625999911254265113123401234401234598346

Sample Output

NOYES

There are three ways of doing this topic:

① dynamic creation of the trie tree, the benefits of this is to save space, no space redundancy, but the disadvantage is the need to new node multiple times, for this problem, each set of test sample new Times up to 10000 * 10 times, to know that dynamic creation of so many nodes is necessarily over 1000ms, But Maybe,hdu's data compare to water, this method still can AC, but this method must pay attention to delete, prevent memory leak.

② static creation of the trie tree, the advantage is to avoid multiple new operations, saving time, but the disadvantage is that there is no guarantee that the data has no space redundancy, and this method actually has some limitations, in some topics, the maximum length of the string is unknown, or the number of strings is unknown, This approach is undesirable, but within this topic, the scope is clear, so this method is feasible.

③ Last method, also I did not think of, first sort the input string, and then compare the adjacent two string is not a prefix.

Method One:

problem:1671 (Phone List) Judge status:acceptedrunid:14381387 language:g++ Author:245747005code Rend Er status:rendered by hdoj g++ Code Render Version 0.01 beta/****************************>>>>headfiles<& lt;<<****************************/#include <cmath> #include <queue> #include <vector># Include <cstdio> #include <string> #include <cstring> #include <iomanip> #include <iostream > #include <sstream> #include <algorithm>using namespace std;/****************************>> >>>define<<<<<*****************************///#pragma COMMENT (linker, "/stack : 1024000000,1024000000 ") #define FIN freopen (" Input.txt "," R ", stdin) #define FOUT freopen (" Output.txt " , "W", stdout) #define CASE (T) for (scanf ("%d", &t); t--;) #define REP (i,a,b) for (int i = a;i <= b;i++) #define REP1 (i,a) for (int i = 1;i <= a;i++) #define REP0 ( I,A) for (int i = 0;i < a;i++) #define MP (A, B) Make_pair (A, b) #define PB (a) push_back (a) #define FST first# Define SND second#define Lson l,mid,rt<<1#define Rson mid+1,r,rt<<1|1/****** >>>>>>debug<<<<<<****************************/# Define out (x) cout<<x<< ""/****************************>>>>separator<<<<*****    /const int maxk = 10;const int maxl = 200+5;int n,t;bool suc;struct node{int cnt;    node* PNEXT[MAXK];    Node (): CNT (0) {rep0 (i,maxk) pnext[i] = NULL;    }};struct trie{node* Proot;    Trie (): Proot (new node ()) {} void Fuck () {proot = new node ();p root->cnt = 0;}    void Addword (const char str[],int len); void Release (const Node *p);};    void Trie::addword (const char Str[],int len) {node* ptr = proot;    BOOL flag = FALSE;        for (int i = 0;i < len;i++) {int nPos = str[i]-' 0 ';        int son_cnt = 0;        for (int j = 0;j < maxk;j++) if (ptr->pnext[j]! = NULL) son_cnt + = ptr->pnext[j]->cnt;        if (ptr->cnt-son_cnt >= 1) {suc = false; return;}        if (ptr->pnext[npos] = = NULL) Ptr->pnext[npos] = new Node (), flag = true;        ptr->cnt++;    PTR = ptr->pnext[npos];    } if (!flag) suc = false; ptr->cnt++;}    void Trie::release (const node* p) {for (int i = 0;i < maxk;i++) if (p->pnext[i]! = NULL) Release (P->pnext[i]); Delete p;}    Char Buf[maxl];int main () {//FIN;        Case (T) {Trie dic;        Dic.fuck ();        Suc = true;        scanf ("%d", &n);            REP1 (i,n) {scanf ("%s", buf); if (suc) dic.        Addword (Buf,strlen (BUF)); } dic.        Release (Dic.proot); printf ("%s\n", suc?)    YES ":" NO "); } return 0;}

Method Two:

problem:1671 (Phone List) Judge status:acceptedrunid:14383224 language:g++ Author:245747005code Rend Er status:rendered by hdoj g++ Code Render Version 0.01 beta/****************************>>>>headfiles<& lt;<<****************************/#include <cmath> #include <queue> #include <vector># Include <cstdio> #include <string> #include <cstring> #include <iomanip> #include <iostream > #include <sstream> #include <algorithm>using namespace std;/****************************>> >>>define<<<<<*****************************///#pragma COMMENT (linker, "/stack : 1024000000,1024000000 ") #define FIN freopen (" Input.txt "," R ", stdin) #define FOUT freopen (" Output.txt " , "W", stdout) #define CASE (T) for (scanf ("%d", &t); t--;) #define REP (i,a,b) for (int i = a;i <= b;i++) #define REP1 (i,a) for (int i = 1;i <= a;i++) #define REP0 ( I,A) for (int i = 0;i < a;i++) #define MP (A, B) Make_pair (A, b) #define PB (a) push_back (a) #define FST first# Define SND second#define Lson l,mid,rt<<1#define Rson mid+1,r,rt<<1|1/****** >>>>>>debug<<<<<<****************************/# Define out (x) cout<<x<< ""/****************************>>>>separator<<<<*****    /const int maxk = 10;const int maxl = 20+5;int n,t;bool suc;struct node{int cnt;    node* PNEXT[MAXK];    Node (): CNT (0) {rep0 (i,maxk) pnext[i] = NULL;    }}memory[100006];int g_cnt;struct trie{node* proot;    Trie (): Proot (New Node ()) {} void Addword (const char str[],int len); void Release (const Node *p);}    Trie;void Trie::addword (const char Str[],int len) {node* ptr = proot;    BOOL flag = FALSE;    for (int i = 0;i < len;i++) {int nPos = str[i]-' 0 ';    int son_cnt = 0;        for (int j = 0;j < maxk;j++) if (ptr->pnext[j]! = NULL) son_cnt + = ptr->pnext[j]->cnt;        if (ptr->cnt-son_cnt >= 1) {suc = false; return;}        if (ptr->pnext[npos] = = NULL) Ptr->pnext[npos] = &memory[g_cnt++],flag = true;        ptr->cnt++;    PTR = ptr->pnext[npos];    } if (!flag) suc = false; ptr->cnt++;}    Char Buf[maxl];int main () {//fin;        Case (T) {memset (memory,0,sizeof (Memory));        g_cnt = 0;        Trie.proot = &Memory[G_Cnt++];        Suc = true;        scanf ("%d", &n);            REP1 (i,n) {scanf ("%s", buf); if (suc) trie.        Addword (Buf,strlen (BUF)); } printf ("%s\n", suc?)    YES ":" NO "); } return 0;}

Method Three:

/****************************>>>>headfiles<<<<****************************/#include <cmath> #include <queue> #include <vector> #include <cstdio> #include <string> #include <cstring> #include <iomanip> #include <iostream> #include <sstream> #include <algorithm> Using namespace std;/****************************>>>>>define<<<<<***************** #pragma COMMENT (linker, "/stack:1024000000,1024000000") #define FIN freopen ("Input.txt", "R", Stdi N) #define FOUT freopen ("Output.txt", "w", stdout) #define CASE (T) for (scanf ("%d", &t); t--;) #define REP (i,a,b) for (int i = a;i <= b;i++) #define REP1 (i,a) for (int i = 1;i <= a;i++) #define REP0 ( I,A) for (int i = 0;i < a;i++) #define MP (A, B) Make_pair (A, b) #define PB (a) push_back (a) #define FS T first#define snd second#define Lson l,mid,rt&Lt;<1#define Rson mid+1,r,rt<<1|1/****************************>>>>>>debug<<&lt ; <<<****************************/#define OUT (x) cout<<x<< ""/****************************&gt ; >>>separator<<<<****************************/string str[10010];int T,n;bool judge (int n) {int    I, J;    int Len;        for (i = 0; i < n-1; i++) {len = Str[i].size ();        for (j = 0; J < Len; j + +) if (str[i][j]! = str[i + 1][j]) break;    if (j >= Len) return false; } return true;    int main () {FIN;        Case (T) {scanf ("%d", &n);        for (int i = 0; i < n; i++) cin >> Str[i];        Sort (str, str + n);        if (judge (n)) printf ("yes\n");    else printf ("no\n"); } return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 3630/hdu 1671 Phone List "Trie dictionary tree dynamic Creation & static Creation"

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.