1. 程式人生 > >poj 3630 / hdu 1671 Phone List 【Trie字典樹 動態建立&靜態建立】

poj 3630 / hdu 1671 Phone List 【Trie字典樹 動態建立&靜態建立】

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 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 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

2
3
911
97625999
91125426
5
113
12340
123440
12345
98346

Sample Output

NO
YES

這個題目有三種做法:

①動態建立Trie樹,這樣做的好處是節省空間,沒有空間冗餘,但是缺點是需要多次new一個節點,對於這個題而言,每組測試樣例 new 的次數多達10000*10次,要知道動態建立這麼多個節點是必然超了1000ms的,但是maybe,hdu的資料比較水,這種方法依舊能AC,但是這種方法必須要注意delete,防止記憶體洩露。

②靜態建立Trie樹,這樣做的好處是避免了多次new操作,節省了時間,但是缺點是,不能保證資料有沒有空間冗餘,而且,這種方法其實還有一些限制的,在有些題目中,字串的最大長度未知,或者字串的數目未知的情況下,這種做法是不可取的,但是在這個題目裡面,都明確了範圍,故這種方法可行。

③最後一種方法,也是我沒有想到的,先對輸入字串進行排序,然後比較相鄰兩個字串是不是字首。

方法一:

Problem : 1671 ( Phone List )     Judge Status : Accepted
RunId : 14381387    Language : G++    Author : 245747005
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
/****************************>>>>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",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();pRoot->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;
}

方法二:
Problem : 1671 ( Phone List )     Judge Status : Accepted
RunId : 14383224    Language : G++    Author : 245747005
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
/****************************>>>>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",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;
}

方法三:
/****************************>>>>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",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<<<<****************************/
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;
}